Code Smells
- Categories
- Complexity
A code smell is a surface indication in code that usually corresponds to a deeper design problem. It is a heuristic, not a rule: a smell tells you where to look and that something may be worth improving, not what is wrong or how to fix it. Named smells (duplicated code, long function, large class, feature envy, divergent change, shotgun surgery, data clumps, primitive obsession) give a shared vocabulary for design problems that are otherwise hard to point at.
Why it Matters
Smells make design quality observable. They turn "this feels bad" into a named, discussable thing, and they connect a visible symptom to its underlying cause: divergent change points at weak cohesion, shotgun surgery at scattered coupling, duplicated code at a missing abstraction. The name is the bridge from noticing to acting.
Signals
- One module changes for many unrelated reasons (divergent change) or one logical change touches many modules (shotgun surgery).
- The same knowledge restated in several places (duplicated code).
- A method more interested in another object's data than its own (feature envy); the same cluster of parameters passed everywhere (data clumps).
Benefits
A fast, low-cost way to spot where a design is decaying, a shared language for code review, and a starting point that maps symptoms to the structural concepts (coupling, cohesion, connascence) that explain them.
Risks
Treating smells as rules turns them into dogma: not every long function or duplication is wrong, and "fixing" a non-problem adds complexity. The judgment is whether the smell signals real pain in changing the code; the nose can be wrong.
Tensions
A smell only suggests that something might be wrong; acting on every one mechanically is its own mistake. Sensitivity must be calibrated by whether the code is actually hard to change, not by the smell's presence alone.
Examples
Shotgun surgery (changing a tax rule forces edits across ten modules) is the symptom; the cause is coupling that should be localized. Duplicated validation logic in client and server is the symptom; the cause is a missing single source of truth.