Lazy Class
A class that doesn’t do enough to justify existence.
Fowler & Beck
Signs of Use (Symptoms)
- Class has only one or two trivial methods.
- Class was created for “future use” that never came.
Why it is bad (Consequences)
- Overhead: Extra file to navigate and maintain.
- Indirection: Makes codebase harder to understand.
Why it happens (Root Cause)
Refactoring reduced a class’s responsibilities but didn’t remove it. Or, speculative design created empty abstractions.
When it might be okay (Exceptions)
- Placeholder for planned expansion.
Explanation
Problem
StringHelper class with one method: capitalize().
The Flaw
Not enough behavior to deserve a class. Just inline it or use a utility.
Real world analogy
Hiring a full-time employee to just answer the phone once a week.
Refactoring Solution
- Inline Class: Merge into another.
- Collapse Hierarchy: If it’s a trivial subclass.
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)