YAGNI
YAGNI (You Aren’t Gonna Need It)
Definition
YAGNI stands for You Aren’t Gonna Need It. It is a principle often associated with Extreme Programming (XP) and Agile methodologies, which states that you should not add functionality until it is necessary.
“Always implement things when you actually need them, never when you just foresee that you need them.” – Ron Jeffries
Why It Matters
- Reduced Complexity: Developing for hypothetical future requirements can clutter your code with unused functionality.
- Faster Delivery: Concentrating on current needs ensures you don’t invest time in code that might never be used, allowing you to ship features faster.
- Easier Refactoring: It’s simpler to refactor when new requirements appear than to maintain preemptive structures that may never be adopted.
- Cost-Effective: Writing and maintaining unused code wastes development and testing resources.
Example in C
Let’s explore a scenario of an inventory system that might (or might not) need advanced analytics in the future.
Before YAGNI: Premature Feature Implementation
public class InventorySystem
{
// Current requirement: Just store and retrieve item stock levels.
private readonly Dictionary<string, int> _stockLevels = new Dictionary<string, int>();
// FUTURE requirement (maybe): Generate complex reports on stock turnover.
// Let's preemptively implement a bunch of analytics to "be prepared."
private readonly Dictionary<string, List<DateTime>> _itemAccessTimes = new Dictionary<string, List<DateTime>>();
// Storing every item access time for potential analytics
public void AccessItem(string itemId)
{
if (!_itemAccessTimes.ContainsKey(itemId))
{
_itemAccessTimes[itemId] = new List<DateTime>();
}
_itemAccessTimes[itemId].Add(DateTime.Now);
// Existing code to retrieve or decrement stock, etc.
}
public void GenerateTurnoverReport()
{
// Complex logic to analyze _itemAccessTimes
// This is a lot of work, no one asked for it yet!
// ...
}
public void AddStock(string itemId, int quantity)
{
if (_stockLevels.ContainsKey(itemId))
{
_stockLevels[itemId] += quantity;
}
else
{
_stockLevels[itemId] = quantity;
}
}
public int GetStock(string itemId)
{
return _stockLevels.ContainsKey(itemId) ? _stockLevels[itemId] : 0;
}
}Observations
- Unnecessary Complexity: We have an entire analytics setup (
_itemAccessTimes,GenerateTurnoverReport()) that isn’t currently needed.
- Maintenance Overhead: We must maintain, test, and potentially refactor this code for analytics—even though it might never be used.
- Bloating the Codebase: This extra code clutters the class and distracts from the real, current functionality: tracking stock levels.
After YAGNI: Implementing Only What Is Needed
public class InventorySystem
{
private readonly Dictionary<string, int> _stockLevels = new Dictionary<string, int>();
public void AddStock(string itemId, int quantity)
{
if (_stockLevels.ContainsKey(itemId))
{
_stockLevels[itemId] += quantity;
}
else
{
_stockLevels[itemId] = quantity;
}
}
public int GetStock(string itemId)
{
return _stockLevels.ContainsKey(itemId) ? _stockLevels[itemId] : 0;
}
// Note: No analytics features implemented until there's an actual requirement.
}Improvements
- Focus on Present Requirements: The code does exactly what’s needed—add and retrieve stock.
- Easier to Understand & Maintain: There’s no extra burden of analytics code, making the class simpler to review and test.
- Prepared for Future: If analytics do become an actual requirement, we can introduce it at that time, basing our design on real usage patterns.
Guidelines for Applying YAGNI
- Implement Features On-Demand: Add capabilities only when there is a real, current need.
- Resist “Just-in-Case” Code: Avoid writing code for hypothetical scenarios that might never happen.
- Iterative Development: Use Agile or iterative approaches where you can introduce new functionality in smaller increments.
- Refactor for Real Requirements: When a legitimate need arises, add or refactor features to accommodate it.
- Balance with Forward Thinking: While YAGNI discourages over-engineering, keep your design flexible enough to extend—just don’t overbuild.
Conclusion
YAGNI helps maintain a lean, focused codebase by preventing unneeded features from creeping in. This principle keeps your development process efficient and your software simpler to maintain. When a new requirement truly emerges, you can address it with targeted design and implementation—rather than supporting speculative features that might never be used.



