YAGNI and Pattern Selection
You'll learn how the "You Aren't Gonna Need It" principle helps you avoid applying design patterns too early, before requirements actually justify them.
What is YAGNI?
YAGNI stands for "You Aren't Gonna Need It"—a principle warning against building functionality or structure you think you'll need in the future. When it comes to design patterns, YAGNI means: don't add a pattern until requirements prove it's necessary.
The Pattern Selection Trap
You've learned many patterns: Singleton, Factory, Strategy, Observer, and more. With this knowledge comes temptation—seeing every problem as a nail for your pattern hammer. You might think: "This could need multiple strategies someday, so I'll add Strategy Pattern now." But YAGNI asks: "Does it need that today?"
Analogy: Imagine buying a home organization system with 47 specialized compartments when you only have 5 items to store. You're paying complexity cost for future needs that may never materialize.
Applying YAGNI to Patterns
Start simple. If you have one algorithm, write one function. Don't create a Strategy Pattern for "future flexibility." When a second algorithm appears and you see duplication or switching logic, then refactor to Strategy.
Current requirements should drive pattern adoption:
- One implementation? → No pattern needed yet
- Two implementations with switching logic? → Consider Strategy or State
- Three subsystems making code messy? → Maybe Facade
If requirements change tomorrow requiring that pattern? Great—refactor then. Code is meant to evolve.
Why This Matters
Premature pattern application creates unnecessary complexity, harder-to-understand code, and maintenance burden. Patterns solve real problems, not hypothetical ones.
Key Takeaway: Apply design patterns when current requirements justify them, not when you speculate about future needs—let YAGNI guide your pattern selection to avoid over-engineering.