Code Smells: Warning Signs for Refactoring
| Cue (Questions / Keywords) | Notes (Details & Explanations) | Summary (Key Takeaway) |
|---|---|---|
| What are Code Smells? | - Surface indicators in code that suggest deeper design problems. - Not bugs → program still works. - They increase complexity, duplication, fragility, or opacity. - Concept popularized by Martin Fowler & Kent Beck. | Code smells = symptoms of poor design, not immediate errors. |
| Why Care About Smells? | - Smelly code → harder to maintain, extend, and test. - Increases technical debt and slows teams. - Early detection saves cost of later rewrites. - Signals when to refactor. | Smells are early warnings before the code rots. |
| Categories of Smells | - Bloaters → large classes/functions/parameter lists. - Object-Orientation Abusers → misuse of inheritance, feature envy, data clumps. - Change Preventers → hard to modify, shotgun surgery, divergent change. - Dispensables → dead code, speculative generality, duplicate code. - Couplers → excessive dependencies, inappropriate intimacy, message chains. | Smells grouped into 5 main categories, each pointing to a design weakness. |
| Common Examples | - Long Method: hard to read & maintain. - Large Class: tries to do too much. - Duplicated Code: copy-paste logic everywhere. - Feature Envy: method uses more data from another class. - Data Clumps: same data always passed together. | Typical smells include long, large, duplicated, or misplaced logic. |
| How to Handle Smells? | - Use refactoring techniques: Extract Function, Introduce Parameter Object, Move Method, Replace Inheritance with Delegation, etc. - Small safe steps with tests. - Don’t refactor blindly → prioritize based on impact. | Smells guide which refactoring technique to apply. |
| Mindset for Smells | - Smells ≠ bad developer, it’s natural as code evolves. - Smells are feedback → code asking for design care. - Not every smell needs immediate fixing — apply judgment. - Continuous attention prevents big rewrites. | Treat smells as signals, not stigma. |