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
- Create a new method
- New method
- Copy the code you want to extract directly into the new method
- Adjust parameters according to requirements
- Adjust old code
- Comment The original code, give yourself a chance to regret
- Call New method
- Test
- Optimized 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
- Test
- Modify 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
Code refactoring principles-important for later reference