PHP Chat "Refactoring-improve the design of existing code" one of the re-organizing your functions _php skills

Source: Internet
Author: User

Mind Map Click the image below, you can see the larger picture.
introduce

I write down my favorite and more interesting places to share with you. Last time I wrote a "PHP dialogue with the boss." Still have many questions, this book has helped me a lot of busy.

If you are busy, or lazy to read text, suggest you directly to see the screenshot, there will be a lot of harvest. You can tell by comparing the code in the screenshot to see which one is superior.

Code part why do I use diagrams? Because I often use mobile phone to see the Code, blog Park code in the phone is a mess, or look at the picture more comfortable.

Professional Terminology

After all, we use the English alphabet code, so with some English words, more can show our professionalism. The following English words, if you have mastered, and other coder communication will be more direct, more professional. --smelly show a bit, hehe.
"*" is often mentioned in the text

Inline: Inline
Function: Functions
*method: Method
Finely grained: fine-grained
Rename: Renaming
Query: Querying
Temp: temporary (temporary)--generally refers to temporary variables
*extract: Extract-I personally prefer to translate to "refine"
*duplicate: Copying
Split: Anatomy
Variable: variable
Factor: Factors, factor

Refactoring Principles

First, what is refactoring?
Noun form: An adjustment to the internal structure of software, which aims to improve the understanding and reduce the cost of modification without changing the performance of the software.
Verb form: Use a series of refactoring criteria to adjust the structure of the software without changing the observed behavior.

Second, why restructure?
1, often refactoring allows code to maintain the form.
2, let the code find the right location.
3, make the software easier to understand.
4, can find the bug.
5, improve our coding speed.
Third, the problem of reconstruction
1, modify the interface naming
If the method in your class is public, then you're at a great risk at rename, and you don't know exactly which modules are calling your method (we often do grep under the entire project, and then look at each module's invocation and logic). -So we write the class regardless of whether it is a property or method to try to be private, to avoid the interface open.

2, when should not be reconstructed
(1) Rewrite all the code, and the existing code is so confusing that refactoring might as well rewrite it.
(2) When the project is nearing the end, refactoring should be avoided. We can put refactoring into phase two to solve.

bad taste of code

First, Duplicate Code
1, the same class, two methods contain the same expression.
Workaround: You can refine the duplicate code extract method, and then let both methods call this extract approach.
2, two classes, there are similar methods.
Workaround: (1) The method of two classes is proposed to construct a parent class together.
(2) Remove the method of one of the classes and invoke the method of the other class.
Two, Long method
1. Short function: Code reading is a bit of an effort, because we have to switch contexts often to see what subroutines do. But the real key to making small method easy to understand is a good name. Readers can learn the function by name, without having to look at what it says. In earlier programming languages, calling methods required extra overhead, which made coder reluctant to use small method. But modern Oo languages are almost entirely exempt from the extra overhead (function calls) in the process.

2. Note Place to refine the signal: whenever you feel the need to explain something with a note, we write something that needs to be explained into a separate function and name it for its purpose. You can do this for a group or even a single line of code. -As long as the function name can explain its users, we should not hesitate to do so.

"Function" is understood as "what to Do" or "how to do"

3. Conditions and loops are often used to refine signals.

4, "Code neat Way" an example. We can think about it!

Third, Large Class

1, the class within a number of property variables have the same prefix or Word end, you can use extract class.

2, the class is not the majority of variables using property variables, you can use extract class.
  
3, there are too many code, can be extract Class.

Four, Long Parameter
Made into introduce Parameter Object. I don't quite agree with that, because when I use other people's methods, I rarely look at code practice, let alone look at the attributes or methods that are used in the object, and take the data I want.

V. Switch statements
1, less use switch statements. -The problem is duplication. When you add a new case, you must find all the cases and modify them.
  
2, to replace it with polymorphism. Procedure: 1. Switch to extract Method;2.movemethod put the practice code in the case into the Polymorphic class.

VI. Comments
Try extract method, if not yet, try rename method.

When you feel you need to write a comment, try refactoring first and try to make all annotations redundant.

Comments are generally used for future purposes and can also be used in areas where you are not fully confident (why do something).


Re-organize your functions

Long method often contains too much information, which is obscured by intricate logic and is difficult to identify.

First, Extract method

Status: I see an too long function, or code that requires a comment to understand the purpose, then put the code in a separate function and let the function name explain the purpose of the change function.

Motivation:

Short and well named functions:--finely grained

1. Great opportunity for reuse.

2, the function reads like reads a series of comments.

3, the function is easy to write.

Emphasis: The key of function length is the semantic distance between function name and function ontology. If the refinement action can harden the clarity of the code, do it.

Practice:

1, create a new function, named after the purpose of the function--what it does, rather than how it is named.

= Even if the Extract function is very simple, such as just a message or function call, you should refine it as long as the new function is able to show the intent of the code in a better way. But if you can't think of a more meaningful name, don't move it.

2, move the extract code from the source function to the new function.

Second, Inline method

When the body of method is as clear and understandable as method name, please inline method.

Third, Inline Temp

A temporary variable that is assigned only once by a simple expression, and is only used once when the assignment is finished. --Please inline Temp

Four, Replace Temp with Query

If a temp variable, save an expression, extract the expression to method. --This is the so-called query style.

Motivation:

1, local variables can make the code difficult to refine.

2. Temporary variables will drive you to write longer code. If you change to query method, then method under class can get this information. --a clearer code will be written.

3. Replace Temp with query is often an essential step before you use Extract method.

Practice:

1. Find temporary variables that are assigned only once.

=> if the temporary variable is assigned more than once, consider using the split temporary variable to split it into multiple variables.

2, the right portion of the assignment to the TEMP variable, extract into a separate function.

=> the method as private and then releases it (public or protected) if another class is in use later.

  

If your code is well-organized, you can often find more efficient optimization scenarios. ———— If the performance is really bad, it's easy to put it back.
V. Introduce explaining Variable
Put the results of a complex expression (or parts of it) into a temporary variable, which explains the purpose of the expression.

Motivation:
Expressions are complex and difficult to read. In this case, a temporary variable can help you decompose the expression into a more manageable form.
  
Six, Split Temporator Variable
A temporary variable is assigned more than once, and it is neither a loop variable nor a collection variable. So for each assignment, create a separate, corresponding temporary variable.

Motivation:

1. If a temporary variable assumes multiple responsibilities, it should be replaced with multiple temporary variables. Each variable assumes only one responsibility.

2, the same temporary variable to bear two different things, will make review become confused.

Six, Remove Assignments to Parameters
If your code assigns a parameter, the position of the parameter is replaced with a temporary variable .

Replace method and Method Object

Large functions cannot use Extract method for local variables. Then put this method into a single object, so that the local variable becomes the object's filed, and then the large function is decomposed into several small methods in the same object.

Motivation:

1, the relatively independent code from the large method extract out, you can greatly improve the readability of the code.

2, a method, local variables overrun, decomposition of this function will be very difficult.

3. Replace method and method object will transform all local variables into the object's domain. The new object is then extract method.

Eight, substitute algorithm
If you want to replace an algorithm with another clearer algorithm, replace the method body with another algorithm . --The original method is directly modified body.
Motivation: As you learn more about the problem, you find that one thing can have a clearer way, and you should replace the complex approach in a clearer way.

Summarize

This is only part of the book, I know there will be a lot of coder should have different views, I am also, some very agree, and some I do not agree with. So to "then its good and from it, its poor and change it."

You are welcome to publish your views.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.