On Refactoring
Robert Fowler in his influential book, “Refactoring,” has a chapter devoted to code smell. It refers to code that needs rewriting to make code human-readable and its maintenance error-free. It permits maintainers to identify where SOLID principles may be applied.SOLID1 means
- The Single-reponsibility principle
- The Open-closed principle
- The Liskov substitution principle
- The Interface segregation principle
- The Dependency inversion principle
“Code smell” is an unfortunate term because programmers do in fact take pride in their work and being told their code is smelly risks offense. I would prefer using “code rot” or “technical debt” to keep emotions in check.
Nevertheless, for my own quick reference, here are the code smells with their page numbers and solutions. There is no point in trying to understand this list without having Mr Fowler’s book in hand.
Reference
Fault | Page | Short Description |
---|---|---|
Mysterious Name | 71 | |
Duplicated Code | 72 | |
Long Function | 72 | |
Long Parameter List | 73 | |
Global Data | 74 | |
Mutable Data | 75 | Updates to data can fail in another part of the programme |
Divergent Change | 75 | Violation of Single-reponsibility Principle |
Shotgun Surgery | 76 | A change requires updates all over the place |
Feature Envy | 77 | A function in one module spends too much time using data in another |
Data Clumps | 78 | Bunches of data found repeatedly everywhere |
Primitive Obsession | 78 | Refusal to create “meaningful types” |
Repeated Switches | 78 | Switch is not evil, but repeated switches are |
Loops | 79 | |
Lazy Element | 79 | Too much structure for a simple task |
Speculative Generality | 80 | “Gosh, we might need to do this someday” |
Temporary Field | 80 | A class has a field that isn’t used all that much |
Message Chains | 80 | A function traverses a chain of objects |
Middle Man | 80 | Too much delegation to other classes |
Insider Trading | 81 | |
Large Class | 81 | Too many fields. May also violate S in SOLID |
Alternative Classes with Different Interfaces | 82 | Possibly a violation of the Interface Segregation Principle |
Data Class | 83 | Class has no methods other than getters and setters |
Refused Bequest | 83 | Violation of Interface Segregation Principle |
Comments | 83 | Comments should not legitimize bad code |