YAGNI Principle

You Aren't Gonna Need It

Overview

YAGNI stands for "You Aren't Gonna Need It". This principle states that you should not add functionality until it is actually needed. Don't build features based on speculation about future requirements.

YAGNI is a core principle of Agile development. It helps prevent over-engineering and keeps the codebase lean and focused.

Key Concepts

Don't implement features until needed

Avoid speculative generalization

Focus on current requirements

Reduces wasted development time

Keeps codebase smaller and simpler

Code Example

java
1// ❌ BAD: YAGNI violation - Building for imaginary requirements
2class User {
3    private String name;
4    private String email;
5    private String phone;
6    private String fax;           // Who uses fax anymore?
7    private String pager;         // Really?
8    private String secondaryEmail;
9    private String tertiaryEmail;
10    private Map<String, String> customFields;  // "Just in case"
11    private List<Address> addresses;  // Currently only need one
12    private TimeZone preferredTimeZone;  // Not in requirements
13    private Locale preferredLocale;  // Not in requirements
14    private Currency preferredCurrency;  // Not in requirements
15    
16    // 50+ getters and setters for features no one asked for
17}
18
19// ✅ GOOD: Only what's needed now
20class User {
21    private String name;
22    private String email;
23    private String phone;
24    private Address address;
25    
26    // Getters and setters for actual requirements
27}
28
29// ❌ BAD: Building abstract framework "for the future"
30interface DataExporter {
31    void export(Object data, ExportFormat format, 
32                ExportOptions options, EncryptionConfig encryption);
33}
34
35// When all you need is:
36// ✅ GOOD: Just export to CSV
37class CsvExporter {
38    public void exportToCsv(List<User> users, String filename) {
39        // Simple CSV export
40    }
41}

The first User class has many fields no one asked for. The second has only what's actually needed.

Advantages

  • Faster development of needed features

  • Less code to maintain

  • Smaller, more focused codebase

  • Easier to understand the system

  • Reduced technical debt

Best Practices

  • 1.

    Implement only what's in current requirements

  • 2.

    Question "what if we need..." statements

  • 3.

    It's easier to add features than remove them

  • 4.

    Trust that you can add it later when needed

  • 5.

    Focus on delivering value now

💡 Interview Tips

  • Explain speculative generalization problems

  • Show how unused code becomes maintenance burden

  • Discuss balance with designing for extensibility

  • Connect to Agile principles

AI Tutor

Ask about the topic

Sign in Required

Please sign in to use the AI tutor

Sign In
YAGNI Principle - Design Principles | LLD | Revise Algo