Mind Mapping
Click, you can see the big picture.
Introduction
I write down the places I like and pay more attention to and share them with you. Last time I wrote a "PHP with the boss of the dialogue." Still have a lot of questions, this book helped me a lot of busy.
If you are busy, or too lazy to read the text, suggest that you directly see, there will be a great harvest. You can know what is better by comparing the code.
The Code section Why do I use the diagram? Because I often use the phone to see the code, the Code of the blog yard in the mobile phone mess, or look at the picture is more comfortable.
Professional Terminology
After all, we use English letters to encode, so we can use some English words to show our professionalism. The following English words, if you have mastered, and other coder communication will be more direct, more professional. --smelly show, hehe.
"*" indicates a common reference in the text
Inline: Inline
Function: Functions
*method: Method
Finely grained: fine-grained
Rename: Renaming
Query: Querying
Temp: temporary (temporary)--general refers to temporary variables
*extract: Extract-I personally prefer to translate into "refinement"
*duplicate: Copy
Split: Anatomy
Variable: variable
Factor: Factor, factor
Refactoring Principles
First, what is refactoring?
Noun form: An adjustment to the internal structure of software, in order to improve its understandable type and reduce its modification cost without changing the observed behavior of software.
Verb form: A series of refactoring criteria are used to adjust the structure of the software without changing its observed behavior.
Second, why restructure?
1. Regular refactoring allows the code to maintain the pattern.
2. Let the code find the right location.
3, make the software easier to understand.
4, you can find the bug.
5, improve the speed of our coding.
Three, the problem of reconstruction
1. Modify interface naming
If the methods in your class are public, then you risk a lot at rename, and you don't know exactly what modules are calling your method (we often do the grep operation under the whole project and then look at the calls and logic of each module individually). --So when we write the class, whether it is a property or a method as private as possible, to avoid the interface open.
2, when should not be reconstructed
(1) Rewrite all the code, and the existing code is too confusing, refactoring is not as good as rewriting.
(2) When the project is nearing completion, the refactoring should be avoided. We can put the refactoring into phase two to solve.
bad taste of the 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 method.
2, two classes, there are similar methods.
Workaround: (1) Propose a method of two classes to construct a parent class together.
(2) Remove the method of one of the classes and call the method of the other class.
Two, Long Method
1, Short function: Code reading cost a bit of effort, because we must often change the context to see what the subroutine did. But the real key to making small method easy to understand is a good name. The reader can understand the function by name, without having to look at what is written in it. In earlier programming languages, calling methods required additional overhead, which made coder reluctant to use small method. But the modern OO language is almost completely exempt from the extra overhead (function calls) within the process.
2, note the local refining signal: whenever the feeling needs to be annotated to explain what the point, we will need to explain the things in a separate function, and its use named. You can do this for a group or even just one 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 also refined signals.
Third, Large Class
1, the class within a number of attribute variables have the same prefix or the end of the word, you can use extract class.
2, not most variables in Class use attribute variables, you can use extract class.
3, there are too many code, can extract Class.
Four, Long Parameter
Made introduce Parameter Object. I don't quite agree with that, because when I use other people's methods, I seldom go to the code practice, not to mention the use of the object's properties or methods, to take the data I want.
V. Switch statements
1, less use switch statement. --the problem is duplication. When you add a new case, you must find all the cases and modify them.
2. Replace it with multiple states. Procedure: 1. Switch is extract Method;2.movemethod put the practice code in the case into the Polymorphic class.
Liu, Comments
Try extract method, if not, then you try rename method.
When you feel the need to write a comment, try refactoring first, and try to make all comments redundant.
Annotations are generally intended for future use and can also be used in areas where you are not fully confident (why do you do something).
re-organize your functions
Long method often contains too much information, which is obscured by the intricate logic, difficult to identify.
One, Extract Method
Put this code in a separate function and let the function name explain the purpose of the change function
Motivation:
A short, well-named function:
1, function finely grained, each other reuse opportunity is relatively big.
2. The function reads like a series of comments.
3, the function is easy to overwrite.
Practice:
1. Create a new function, name it according to the intent of the function--name it "What to do," 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 can be a better way to reveal the intent of the code." But if you can't think of a more meaningful name, don't touch it.
2. Move the extract code from the source function to the new function.
Second, Inline Method
When the method body is as clear as the method name, please use the inline method.
Third, Inline Temp
A temporary variable, which is assigned only once by a simple expression, and is used only once when the assignment is complete. --Please inline Temp
Iv. Replace Temp with Query
If a
Temp
variable, save an expression that will
Extract Method
.
--
This is called the query type,
Query
Motive:
1, local variables will make the code difficult to refine.
2, temporary variables will drive you to write longer code. If you change to query method, then the method under class can obtain this information. --will write a clearer code.
3. Replace Temp with query is often an essential step before you apply Extract method.
Practice:
1. Find temporary variables that are only assigned once.
= = If the temporary variable is assigned more than once, consider splitting it into multiple variables using split temporary variable.
2, the right part of the assignment to the TEMP variable, extract into a separate function.
= = Declare method as private and release it (public or protected) later if there is another class.
If the code is well organized, you can often find more efficient optimization scenarios. ———— If the performance is really bad, then it's easy to put it back. Www.2cto.com
Wu, introduce explaining Variable
put the result of a complex expression (or part of it) in a temporary variable that explains the purpose of the expression with this variable name.
Motive:
Expressions are complex and difficult to read. In this case, temporary variables can help you break down an 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.
Motive:
1. If a temporary variable takes on multiple responsibilities, it should be replaced with more than one temporary variable. Each variable assumes only one responsibility.
2, the same temporary variable to bear two different things, will make review become confused.
VI. Remove Assignments to Parameters
If your code assigns a value to a parameter, then
Replace the position of the parameter with a temporary variable。
Seven, Replace method with method Object
Large functions cannot use Extract method for local variables. So
put this
Method
into a single object, so that the local variable becomes the object's
filed
, and then decompose the large function into a number of small
Method
.
Motive:
1, the relatively independent code from the large method of 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 with method object changes all local variables to the domain of the object. The new object is then extract method.
Viii. Substitute algorithm
If you want to replace an algorithm with another one that is clearer, then
will be
Method Body
to replace with another algorithm。 --is to directly modify the original method Body.
Motivation: As you learn more about the problem, you find that one thing can have a clearer way, you should replace the complex way in a clearer way.
Summary
This is only a part of the book, I know there will be a lot of coder should have different views, I myself also, some very agree, some I also do not agree with. Therefore, "then the good of it, and its poor change."
Welcome everyone to express their views.
Excerpt from Chuanshan Armour
http://www.bkjia.com/PHPjc/478310.html www.bkjia.com true http://www.bkjia.com/PHPjc/478310.html techarticle Mind Map Click, you can see the big picture. Introduce me to the place I like more and more attention to write down and share with you. Last time I wrote a "PHP with the boss of the dialogue." Or is it ...?