Original address: PHP Talk "Refactoring - Improving the Design of Existing Code" Part 1 Reorganize your functions
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, I suggest 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 terminology
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 to "refining"
*duplicate:Copy
split: dissection
variable: variable
factor: factor, factor
Principle of reconstruction
1. What is reconstruction?
Noun form: An adjustment to the internal structure of software, with the purpose of improving its understandability and reducing its modification costs without changing the observable behavior of the software.
Verb form: Use a series 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 encoding 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 grep on the entire project Operation, 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 as private as possible 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) When the project is nearing the end, refactoring should be avoided. We can put the reconstruction into the second phase to solve it.
Bad code smell
1. Duplicate Code
1. Two methods of the same class contain the same expression.
Solution: You can use 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 another class.
2. Long Method
1. Short functions: Code reading takes some effort because we have to frequently switch context 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 required additional overhead, which made 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.
4. An example of "Code Cleanliness". We can think about it!
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
Made into 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 new cases, 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 Extract Method. If that doesn’t work, try Rename Method.
When you feel the need to write comments, please try refactoring first and try to make all comments redundant.
Comments are generally used for future plans, and 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
Situation: I see a function that is too long or code that needs a comment to understand its purpose. Then put this code into a separate function and let the function name explain the purpose of the function.
Motive:
Short and well-named functions: --finely grained
1. Great opportunity for reuse.
2. The function reads like a series of comments.
3. Function overwriting is easy.
Key point: The key to function length lies in the semantic distance between the function name and the function ontology. If refining actions will enhance the clarity of your code, then do it.
Method:
1. Create a new function and name it according to the purpose of the function - name it after "what it does" rather than "how" it does it.
=》 Even if the Extract Function is very simple, such as just a message or a function call, as long as the new Function can express the code intention in a better way, you should also extract it. 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, Extract Method will be used for this expression. ——This is the so-called query formula, query
Motive:
1. Local variables make it difficult to refine the code.
2. Temporary variables will drive 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.
How to do it:
1. Find the temporary variable that is only assigned 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.
use with off ’ through through through use use through through through use use through using using'' through through ’ s' ’ s’’’ through’’s’’’’ through’' ‐‐ ‐‐‐‐‐‐‐‐‐ to to be be in the class's class, be sure to declare the Method as private, and then release it (public or protected) if it is used by other classes in the future.
If the code is well organized, then you can often find more effective optimization solutions. ————If the performance is really bad, it is easy to put it back.
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. Thencreate 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 value to a parameter, then replace the parameter's position with a temporary variable.
7. Replace Method with Method Object
Extract Method cannot be used when using local variables in large functions. Then put this Method into a separate object, so that the local variables become filed of the object, and then decompose the large function into several small Methods 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, there are so many local variables that 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 complicated way with a 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 opinion.