YAGNI Principle
YAGNI Principle: You Aren’t Gonna Need It
Section titled “YAGNI Principle: You Aren’t Gonna Need It”The YAGNI (You Aren’t Gonna Need It) principle is a core principle of Extreme Programming (XP) that states: “Don’t add functionality until it’s actually needed.”
Why YAGNI Principle?
Section titled “Why YAGNI Principle?”YAGNI Principle helps you:
- Avoid over-engineering - Don’t build features you don’t need
- Save time - Focus on what’s actually needed
- Reduce complexity - Less code to maintain
- Faster delivery - Ship working features sooner
- Flexibility - Add features when requirements are clear
Visual: YAGNI Principle Concept
Section titled “Visual: YAGNI Principle Concept”What Happens If We Don’t Follow YAGNI?
Section titled “What Happens If We Don’t Follow YAGNI?”Without YAGNI Principle, you might:
- Over-engineer - Build features that are never used
- Waste time - Spend time on unnecessary code
- Increase complexity - More code = more bugs
- Slower delivery - Takes longer to ship features
- Harder to change - Unused code gets in the way
Visual: The Over-Engineering Problem
Section titled “Visual: The Over-Engineering Problem”Simple Example: User Management System
Section titled “Simple Example: User Management System”Let’s see a simple example showing the problem and solution:
The Problem: Building for Future That May Never Come
Section titled “The Problem: Building for Future That May Never Come”Visual: Over-Engineering Flow
Section titled “Visual: Over-Engineering Flow”# ❌ Without YAGNI - Building features you don't need!
class User: """Over-engineered user class with features you don't need"""
def __init__(self, name: str, email: str): self.name = name self.email = email # Building for future that may never come self.preferences = {} # Don't need this yet! self.social_links = {} # Don't need this yet! self.subscription_tier = None # Don't need this yet! self.notification_settings = {} # Don't need this yet! self.analytics_data = {} # Don't need this yet!
def update_preferences(self, prefs: dict): # Feature you don't need yet! self.preferences.update(prefs)
def add_social_link(self, platform: str, url: str): # Feature you don't need yet! self.social_links[platform] = url
# ... many more methods for features you don't need
# Problems:# - Code is complex for no reason# - Harder to understand what's actually used# - Wasted time building unused features# - Harder to maintain unused code// ❌ Without YAGNI - Building features you don't need!
public class User { // Over-engineered user class with features you don't need private String name; private String email; // Building for future that may never come private Map<String, Object> preferences; // Don't need this yet! private Map<String, String> socialLinks; // Don't need this yet! private String subscriptionTier; // Don't need this yet! private Map<String, Boolean> notificationSettings; // Don't need this yet! private Map<String, Object> analyticsData; // Don't need this yet!
public User(String name, String email) { this.name = name; this.email = email; this.preferences = new HashMap<>(); this.socialLinks = new HashMap<>(); this.notificationSettings = new HashMap<>(); this.analyticsData = new HashMap<>(); }
public void updatePreferences(Map<String, Object> prefs) { // Feature you don't need yet! this.preferences.putAll(prefs); }
public void addSocialLink(String platform, String url) { // Feature you don't need yet! this.socialLinks.put(platform, url); }
// ... many more methods for features you don't need}
// Problems:// - Code is complex for no reason// - Harder to understand what's actually used// - Wasted time building unused features// - Harder to maintain unused codeThe Solution: YAGNI Principle
Section titled “The Solution: YAGNI Principle”Visual: YAGNI Solution Flow
Section titled “Visual: YAGNI Solution Flow”# ✅ With YAGNI - Build only what you need!
class User: """Simple user class - only what you actually need"""
def __init__(self, name: str, email: str): self.name = name self.email = email # That's it! Only what you need right now
def get_name(self) -> str: return self.name
def get_email(self) -> str: return self.email
# When you actually need preferences, add them then!# When you actually need social links, add them then!# Don't build them "just in case"
# Benefits:# - Simple and clear# - Easy to understand# - Fast to implement# - Easy to maintain# - Can add features when actually needed// ✅ With YAGNI - Build only what you need!
public class User { // Simple user class - only what you actually need private String name; private String email;
public User(String name, String email) { this.name = name; this.email = email; // That's it! Only what you need right now }
public String getName() { return name; }
public String getEmail() { return email; }}
// When you actually need preferences, add them then!// When you actually need social links, add them then!// Don't build them "just in case"
// Benefits:// - Simple and clear// - Easy to understand// - Fast to implement// - Easy to maintain// - Can add features when actually neededReal-World Example: API Design
Section titled “Real-World Example: API Design”Here’s a more realistic example:
# ❌ Without YAGNI - Building API endpoints you don't need
class UserAPI: """Over-engineered API with endpoints you don't need"""
def get_user(self, user_id: int): # You need this pass
def create_user(self, user_data: dict): # You need this pass
def update_user_preferences(self, user_id: int, prefs: dict): # You don't need this yet! pass
def get_user_analytics(self, user_id: int): # You don't need this yet! pass
def export_user_data(self, user_id: int): # You don't need this yet! pass
# ✅ With YAGNI - Build only endpoints you need
class UserAPI: """Simple API - only what you actually need"""
def get_user(self, user_id: int): # You need this - build it! pass
def create_user(self, user_data: dict): # You need this - build it! pass
# Don't build other endpoints until you actually need them! # When you need them, add them then.// ❌ Without YAGNI - Building API endpoints you don't need
public class UserAPI { // Over-engineered API with endpoints you don't need public User getUser(int userId) { // You need this return null; }
public User createUser(UserData userData) { // You need this return null; }
public void updateUserPreferences(int userId, Map<String, Object> prefs) { // You don't need this yet! }
public Map<String, Object> getUserAnalytics(int userId) { // You don't need this yet! }
public byte[] exportUserData(int userId) { // You don't need this yet! }}
// ✅ With YAGNI - Build only endpoints you need
public class UserAPI { // Simple API - only what you actually need public User getUser(int userId) { // You need this - build it! return null; }
public User createUser(UserData userData) { // You need this - build it! return null; }
// Don't build other endpoints until you actually need them! // When you need them, add them then.}When to Apply YAGNI?
Section titled “When to Apply YAGNI?”Apply YAGNI Principle when:
✅ Building new features - Only build what’s needed now
✅ Designing abstractions - Don’t abstract until you see duplication
✅ Adding “nice to have” features - Skip them until actually needed
✅ Premature optimization - Don’t optimize until you have performance issues
✅ Future-proofing - Don’t build for hypothetical future needs
Visual: When to Apply YAGNI
Section titled “Visual: When to Apply YAGNI”When NOT to Apply YAGNI?
Section titled “When NOT to Apply YAGNI?”Don’t apply YAGNI when:
❌ Clear requirements - If you know you’ll need it soon, build it
❌ Critical infrastructure - Some things need to be built right
❌ Security concerns - Security features should be built proactively
❌ Technical debt - Sometimes you need to fix architecture issues
Key Takeaways
Section titled “Key Takeaways”- YAGNI Principle = You Aren’t Gonna Need It
- Build only what you need - Don’t add functionality until needed
- Avoid over-engineering - Don’t build “just in case”
- Faster delivery - Ship working features sooner
- Balance - Don’t confuse YAGNI with poor planning
Remember: YAGNI is about avoiding unnecessary features, not avoiding planning. Build what you need, when you need it! 🎯