Dead Code
Unused variables, parameters, methods, files.
Fowler & Beck
Signs of Use (Symptoms)
- Unreachable code after a
returnorthrow. - Functions/classes that are never called.
- Commented-out code blocks left “just in case”.
Why it is bad (Consequences)
- Clutter: Distracts readers.
- Confusion: Is this code important? Is it a bug that it’s not called?
Why it happens (Root Cause)
Code was deleted logically (no callers) but not physically. Fear of “needing it later”.
When it might be okay (Exceptions)
- Debugging code temporarily disabled.
- Feature flags (code is conditionally live).
Explanation
Problem
oldCalculation() was replaced by newCalculation(). oldCalculation() still exists.
The Flaw
No one will ever call oldCalculation(). It’s cruft. Delete it.
Real world analogy
Keeping expired milk in the fridge “just in case”.
Refactoring Solution
- Delete it. Version control (Git) remembers if you need it.
Pros and Cons (of the Antipattern)
| Pros (Why people do it) | Cons (The price you pay) |
|---|---|
Comparison
- Related Antipatterns: Speculative Generality.
- Related Principles: YAGNI.
Code example
Typescript
Bad (The Antipattern)
Good (The Fix)