Mind Map
Click on the image below to see a larger image.
Introduction
I wrote down the things I like and care about most and share them with you. Last time I wrote an article "Dialogue between PHP and the Boss". I still have a lot of questions, and this book helped me a lot.
If you are busy or too lazy to read the text, it is recommended that you just look at the screenshots and you will gain a lot. You can tell which one is better by comparing the code in the screenshot.
Why do I use pictures in the code part? Because I often use my mobile phone to read code, the code in the blog park is messy on my mobile phone, so it is more comfortable to look at pictures.
Professional terms
After all, we use English letters for coding, so using some English words can better show our professionalism. If you master the following English words, you will be more direct and professional when communicating with other coders. ——Let’s show off our stink, haha.
"*" indicates
frequently mentioned in the text
inline: inline
function:function
*method: method
finely grained: finely grained
rename: Rename
query: query
temp: temporary (temporary) - generally refers to temporary variables
*extract: extraction - I personally prefer to translate it as "refining"
*duplicate: copy
split:anatomy
variable: variable
factor: factor, factor
Reconstruction Principle
1. What is reconstruction?
Noun form: An adjustment to the internal structure of software, the purpose is to improve its understandability and reduce the cost of modification without changing the observable behavior of the software.
Verb form: Use a set of refactoring principles to adjust the structure of the software without changing its observable behavior.
2. Why refactor?
1. Frequent refactoring can keep the code in its proper form.
2. Let the code find the appropriate location.
3. Make the software easier to understand.
4. Bugs can be found.
5. Improve our coding speed.
3. The problem of reconstruction
1. Modify the interface name
If the method in your class is public, then when you rename, you take a big risk because you don't know which modules are calling your method (our usual approach is to do a grep operation on the entire project , and then look at the calls and logic of each module one by one). ——So when we write a class, we try to make the attributes and methods private to avoid opening the interface.
2. When not to refactor
(1) Rewrite all the code, and the existing code is too confusing, so refactoring is better than rewriting.
(2) Refactoring should be avoided when the project is nearing the end. We can put the reconstruction into the second phase to solve it.
Bad smell of code
1. Duplicate Code
1. In the same class, two methods contain the same expression.
Solution: You can Extract Method to extract duplicate code, and then let both methods call this Extract Method.
2. Two classes have similar methods.
Solution: (1) Put forward the methods of the two classes and jointly construct a parent class.
(2) Delete the method of one class and call the method of the other class.
2. Long Method
1. Short functions: Code reading takes some effort, because we must often switch contexts to see what the subroutine does. But the real key to making a small method easy to understand is a good name. Readers can understand the function of the function through its name without having to read what is written in it. ——In early programming languages, calling methods requires additional overhead, which makes coders reluctant to use small methods. But modern OO languages have almost completely eliminated the additional overhead (function calls) within the process.
2. Refining signals in comments: Whenever we feel that we need to use comments to explain something, we write what needs to be explained into an independent function and name it according to its purpose. This can be done with a group of codes or even with just one line of code. - As long as the function name explains its user, we should not hesitate to do so.
"Function" is understood as "what to do" or "how to do it"
3. Conditional expressions and loops are often refining signals.
3. Large Class
1. If several attribute variables in Class have the same prefix or suffix, Extract Class can be used.
2. Not most variables in Class use attribute variables, you can use Extract Class.
3. If there is too much code, you can Extract Class.
4. Long Parameter
Make Introduce Parameter Object. ——I don’t agree with this, because when I use other people’s methods, I rarely look at the code practice, let alone look at the properties or methods of the objects used in it to get the data I want.
5. Switch Statements
1. Use less switch statements. ——The problem is duplication. When adding a new case, you must find all cases and modify them.
2. Replace it with polymorphism. Method: 1. Perform Extract Method on switch; 2. MoveMethod puts the practical code in the case into the polymorphic class.
6. Comments
Try using Extract Method. If that doesn’t work, try Rename Method.
When you feel the need to write comments, try refactoring first to make all comments redundant.
Comments are generally used for future plans, but can also be used for areas where you are not completely sure (why you are doing something).
Reorganize your functions
Long Method often contains too much information, which is covered by intricate logic and difficult to identify.
1. Extract Method
Put this code into a separate function and let the function name explain what the function does
Motive:
Short, well-named functions:
1. Functions are finely grained, so there is a greater chance of reusing each other.
2. The function reads like a series of comments.
3. Function overwriting is easy.
Instructions:
1. Create a new function and name it according to the purpose of the function - name it after what it does, not how it does it.
=》Even if the Extract Function is very simple, such as just a message or a function call, you should refine it as long as the new Function can express the intent of the code in a better way. But if you can't think of a more meaningful name, leave it alone.
2. Move the Extract code from Source Function to New Function.
2. Inline Method
When the Method Body is as clear and easy to understand as the Method Name, please Inline Method.
3. Inline Temp
A temporary variable is only assigned once by a simple expression, and is only used once after assignment. ——Please Inline Temp
4. Replace Temp with Query
If a Temp variable holds an expression, this expression will be Extract Method. ——This is the so-called query formula, query
Motive:
1. Local variables make it difficult to refine the code.
2. Temporary variables will force you to write longer code. If it is changed to query method, then the method under the class can get this information. - Will write cleaner code.
3. Replace Temp with Query is often an essential step before you use Extract Method.
Method:
1. Find the temporary variable that is assigned only once.
=> If a temporary variable is assigned more than once, consider using Split Temporary Variable to split it into multiple variables.
2. Extract the right part of the Temp Variable assignment to an independent function.
=> Declare the Method as private, and then release it (public or protected) if it is used by other classes in the future.
If your code is well organized, you can often find more effective optimizations. ————If the performance is really bad, it is easy to put it back. www.2cto.com
5. Introduce Explaining Variable
Put the result of a complex expression (or part of it) into a temporary variable, and use the variable name to explain the purpose of the expression.
Motive:
Expressions are complex and difficult to read. In this case, temporary variables can help you break the expression into a more manageable form.
6. Split Temporator Variable
If a temporary variable is assigned more than once, it is neither a loop variable nor a set variable. Then creates an independent and corresponding temporary variable for each assignment.
Motive:
1. If a temporary variable bears multiple responsibilities, it should be replaced with multiple temporary variables. Each variable has only one responsibility.
2. The same temporary variable is responsible for two different things, which will make the review confusing.
6. Remove Assignments To Parameters
If your code assigns a parameter, replace the parameter's position with a temporary variable.
7. Replace Method with Method Object
Extract Method cannot be used for the use of local variables by large functions. Then put this Method into a separate object. In this way, the local variables become the object's filed, Then break the large function into several small Method in the same object.
Motive:
1. Extracting relatively independent code from a large Method can greatly improve the readability of the code.
2. In a Method, local variables are rampant, and it will be very difficult to decompose this function.
3. Replace Method with Method Object will change all local variables into the value range of the object. Then perform Extract Method on this new object.
8. Substitute Algorithm
If you want to replace an algorithm with another one that is cleaner, then replace the Method Body with another algorithm . ——Just modify the original Method Body directly.
Motivation: As you learn more about the problem and you find that something can be done in a clearer way, you should replace the complex way with the clearer way.
Summary
This is only part of the content of this book. I know that there will be many coders who should have different opinions, including myself. Some of them agree very much, and some of them I don't agree with. Therefore, we must "follow the good and correct the bad."
Everyone is welcome to express your opinions.
Excerpted from Chuanshan Jia