1. Definition
Refactoring in Software engineering (refactoring) is to improve the quality and performance of software by adjusting program code without changing the existing functions of the software, so that the design pattern and architecture of the program are more reasonable, and the expansibility and maintainability of the software are improved.
2. The importance of refactoring. Why to Refactor.
Why to Refactor. Change the way the system is implemented without changing the functionality of the system. Why do you do this? It is a waste of our customers ' investment to devote energy to not satisfying the needs of the customer, but merely to change the way the software is implemented.
There is only one factor that threatens the life of the software: changes in requirements. A software is always created to address a particular need, and as time goes by, the customer's needs always change, which creates a bad phenomenon: the SOFTWARE PRODUCT was originally created, is carefully designed, and has a good architecture. However, as the demand changes, we must constantly modify the original functions, add new functions, there are some shortcomings need to be modified. In order to make changes, it is unavoidable to violate the original design framework. After a period of time, the software architecture is riddled with flaws. More and more bugs, more and more difficult to maintain, the new requirements are increasingly difficult to achieve, the software architecture of the new needs of the gradual loss of support capacity, but become a constraint. Finally, the cost of developing new requirements will outweigh the cost of developing a new software, which is when the life of this software system comes to an end.
Refactoring will maximize the avoidance of such a phenomenon. After the development of the system to a certain stage, the use of refactoring, do not change the external functions of the system, only the internal structure of the reorganization. Through the reconstruction, constantly adjust the structure of the system, so that the system for the change of requirements always have a strong adaptability to extend the software life cycle.
Benefits of Refactoring:
Improved software design makes software easier to understand (layout, naming, annotations, simplifying logic, etc.) can help you find bugs () improve the speed of software development (improve methods, provide advantages for subsequent development) 3. When to refactor. Under what circumstances a refactoring is required.
Refactoring when new features are added. Refactoring when a bug is modified. Refactoring during code review.
The following situations require refactoring: A program with complex conditional logic: The method is too long, the logic is too complex, the error rate goes straight up, the readability is straight down; too large a class is often an unreasonable result of class abstraction, which reduces the reusability of code; A program that has the same block of code in a class, refining a suitable independent method ; The same code block is refined into a new class; The program of transition coupling: modifying or adding a small function to modify a large amount of code; a hard-to-read program: Bad naming, typography, annotations;
When an existing program fails to run, or the refactoring cost has exceeded the rewrite cost, it should be the rewrite program, not the refactoring program
4. Points of attention in refactoring
1. In refactoring you need to be careful about modifying interfaces that have been provided externally, because anything can happen. The best way is to publish a new interface, retain the original interface, the original interface to call the new interface (note: Not the code to copy the new interface, which will lead to code duplication), and then add the old interface on the original annotation "" @Deprecated (deprecated, outdated method), This provides time for the calling interface to be modified.