A code smell is a sign on the surface of the code that usually points to a deeper design problem. Common smells and their usual fixes:
- Long method: use Extract Method to split it into well-named steps.
- Large class or god class (one class that does too much): use Extract Class to split it by responsibility.
- Long parameter list: use Introduce Parameter Object, or pass a whole object.
- Duplicated code: use Extract Method or Pull Up Method, but only if it really is the same knowledge.
- Feature envy, a method that uses another object's data more than its own: use Move Method to move it to that object.
- Primitive obsession, such as strings for emails and numbers for money: replace the primitive with a value object.
- Shotgun surgery, where one change needs edits in many classes: use Move Method or Move Field to bring the logic together.
- Switch on type repeated in several places: use Replace Conditional with Polymorphism.
Smells are hints, not rules. Confirm the real problem before you change anything.
- Fix a long method with Extract Method and a god class with Extract Class.
- Fix a long parameter list with a parameter object.
- Fix feature envy by moving the method to the object whose data it uses.
- Fix primitive obsession with value objects.
- Fix shotgun surgery by bringing the logic together, and fix type switches with polymorphism.
Read more