Code refactoring
Introduction to Code Refactoring
Refactoring purpose
- The same code is best to appear only once
- Primary and Secondary methods
Main method
Contains only sub-methods that implement complete logic
Clear thinking, easy to read
Secondary method
Implement specific logic functions
After the test passes, almost no maintenance is followed
Steps for Refactoring
1 Create a new method
? New method
? Copy the code you want to extract directly into the new method
? Adjust parameters according to requirements
2 adjusting Old Code
? Comment The original code, give yourself a chance to regret
? Call New method
3 Testing
4 optimization Code
? In the original position, because to take care of more logic, the code might be justified
? After extraction, because the code is small, you can check whether you can optimize
? Branching is more nested, not only performing poorly, but not easy to read
5 Testing
6 Modifying comments
? In development, the more comments are not the better
? If the comment is ignored, it may be some time before you can read the comment
? . m key implementation logic, or complex code, need to add comments, otherwise, long time you can not understand!
? All the properties and methods in. h need to have a full comment because the. h file is for the whole team to see
Refactoring must take small steps to change the test
Step 1 Code Refactoring