Reasons to refactor:
- code is duplicated;
- A routine is too long; A routine has a poor name;
- 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; One class is overly intimate with another;
- A parameter list has too many parameters;
- Data members are public;
- Global variables are used;
Refactorings:
- Data-level refactoring;
- statement-level refactoring;
- routine-level refactoring;
- class implementation refactoring;
- class interface refactoring;
- system-level refactoring.
TIPS:
- replace a magic number with a named constant;
- rename a variable with a clearer or more informative name;
- replace an expression with a routine;
- convert a multi-use variable to multiple single-use variables;
- replace a traditional record with a data class;
- change value objects to reference objects;
- create a definitive reference source for data you can't control;
- provide a factory method rather than a simple constructor.