#2: You Aren’t Gonna Need It (YAGNI)
Welcome back to The Coder Cafe! Yesterday, we discussed the pitfalls of premature abstraction. Today, let’s dive into another crucial principle that complements our discussion: YAGNI.
YAGNI is an acronym for You Aren’t Gonna Need It. This principle advises against adding functionality until it is actually needed. The main idea is to avoid over-engineering solutions based on hypothetical future requirements.
Why does YAGNI matter? According to research on software engineering fundamentals, maintenance typically consumes about 40% to 80% of software costs. Given this significant cost, we software engineers must keep that in mind and write code that minimizes maintenance costs.
Adhering to the YAGNI principle by focusing on building what is necessary for the current requirements should be the way to go in most cases. Furthermore, applying YAGNI can lead to these two main benefits:
Reduced complexity: Eliminating unnecessary features and abstractions to keep our codebase simpler.
Better agility: Tackling our codebase complexity is a way to adapt more easily to changing requirements.
Here are five common YAGNI smells1:
Overly abstracted code: An excessive use of abstract classes or interfaces when simpler implementations would be enough.
Unnecessary generics: Use of generic types or methods for functionality that doesn’t require them.
Premature feature implementation: Adding features that aren’t required by the current use case but might be useful in the future.
Unused code: Any code that isn't currently used or code written specifically to support tests but not production needs.
Misplaced optimizations: Applying optimizations to parts of the code that do not impact performance significantly.
YAGNI is a powerful principle that helps us stay focused, ensuring we avoid over-engineering and stick to what is needed right now. By cutting out unnecessary features and abstractions, we keep things simple, reduce complexity, lower maintenance costs, and make it easier to adapt when things change.
However, like any rule, YAGNI should be applied carefully. In some cases, planning ahead is necessary—such as with long-term architectural decisions, anticipated user growth, or regulatory compliance. The key is to find the right balance between addressing current needs and preparing for future ones.
Tomorrow, we will discuss how to avoid applying the YAGNI principle too rigidly.
A smell is an indicator of potential design flaws.