重构是改变软件的内在结构从而让其更好理解更容易修改。在软件开发过程中随处可见它的身影。很多地方都可以用重构来改善,比如重复的代码,太长的函数,嵌套很深的循环等,下面是一个比较完整的list,需要重构的原因:
Checklist: Reasons to Refactor
- Code is duplicated
- A routine is too long
- A loop is too long or too deeply nested
- A class has poor cohesion
- A class interface does not provide a consistent level of abstraction
- A parameter list has too many parameters
- Changes within a class tend to be compartmentalized
- Changes require parallel modifications to multiple classes
- Inheritance hierarchies have to be modified in parallel
- Related data items that are used together are not organized into classes
- A routine uses more features of another class than of its own class
- A primitive data type is overloaded
- A class doesn't do very much
- A chain of routines passes tramp data
- A middle man object isn't doing anything
- One class is overly intimate with another
- A routine has a poor name
- Data members are public
- A subclass uses only a small percentage of its parents' routines
- Com