Design Patterns Anti-Patterns
An anti-pattern is a common solution that looks helpful and causes harm over time. Teams fall into them under deadline pressure or through misunderstanding. Recognizing the warning signs saves hours of painful repair.
The Layman's View
A road shortcut across a lawn saves ten seconds today. After months of foot traffic, the grass dies and mud covers the path. Anti-patterns work like that shortcut: a quick gain now, a larger cost later.
Anti-Patterns Overview
┌──────────────────┬───────────────────────────────────────────┐ │ God Class │ One class does everything │ │ Spaghetti Code │ Tangled flow with no clear structure │ │ Copy-Paste │ Same code duplicated across files │ │ Golden Hammer │ One favorite tool used for every problem │ │ Singleton Abuse │ Global state hidden behind getInstance() │ │ Premature Pattern│ Patterns added before any need exists │ │ Magic Numbers │ Unexplained values scattered in code │ │ Lava Flow │ Dead code nobody dares to remove │ └──────────────────┴───────────────────────────────────────────┘
God Class
One class grows to thousands of lines and handles orders, emails, reports, and payments. Every change risks a break somewhere else.
┌────────────────────────────────┐ ┌────────┐ ┌─────────┐ ┌────────┐ │ ApplicationManager │ ───▶ │ Orders │ │ Mailer │ │Reports │ │ orders, mail, reports, billing │ split └────────┘ └─────────┘ └────────┘ └────────────────────────────────┘
Cure: split by responsibility, following the Single Responsibility Principle.
Spaghetti Code
Control flow jumps between methods with no logic that a reader can follow. Deep nesting and shared variables tie everything together. Cure: extract small, named methods and introduce layers.
Copy-Paste Programming
A developer duplicates a block and tweaks a few lines. Every copy needs each bug fix repeated. Cure: extract shared logic into one method, or use Template Method or Strategy when the copies differ slightly.
Golden Hammer
A team loves one technology or pattern and applies it everywhere. Frameworks chosen for huge projects burden small tools. Cure: match tools to problems, and compare at least two options before choosing.
Singleton Abuse
Developers turn many classes into singletons for convenient access. Hidden global state links unrelated parts and makes tests unreliable.
Tests run in any order? ──▶ Singleton keeps old data ──▶ random failures
Cure: pass objects through constructors with Dependency Injection, and keep true singletons for loggers and configuration only.
Premature Pattern Use
Beginners just learned a pattern and apply it to a simple problem. Three extra interfaces appear for a task a single function could solve. Cure: apply the rule of three, and refactor to a pattern when repeated pain appears.
Magic Numbers and Strings
// Anti-pattern
if (user.level == 3) { ... }
price = price * 0.85;
// Cure
final int PREMIUM_LEVEL = 3;
final double MEMBER_DISCOUNT = 0.85;
if (user.level == PREMIUM_LEVEL) { ... }
price = price * MEMBER_DISCOUNT;Named constants explain intent and give one place for updates.
Lava Flow
Old experiments, unused methods, and commented-out blocks harden into the codebase. Nobody knows whether they matter, and nobody deletes them. Cure: rely on version control history, remove dead code, and confirm with tests.
Spotting Anti-Patterns Early
| Warning Sign | Possible Anti-Pattern |
|---|---|
| A file longer than a thousand lines | God Class |
| Fixing one bug breaks another feature | Spaghetti Code |
| Identical blocks across many files | Copy-Paste |
| Tests pass alone and fail together | Singleton Abuse |
| Nobody can explain a class | Lava Flow |
A Healthy Team Habit
Hold short code reviews and ask one question: "What would change if this requirement changed?" Honest answers expose tangled spots before they harden.
Key Takeaways
- Anti-patterns offer quick wins and costly futures.
- God classes, spaghetti code, and copy-paste are the most common.
- Misused patterns, such as Singleton Abuse, count as anti-patterns too.
- Reviews, tests, and small refactors keep codebases healthy.
