YAGNI

YAGNI — You Aren't Gonna Need It

"Always implement things when you actually need them, never when you just foresee that you need them." — Ron Jeffries, one of the creators of Extreme Programming


Core idea

Don't write code "for the future." Empirical observation on real projects: roughly 80% of speculative code is never used, and the remaining 20% is used in ways you didn't predict.


Why it matters

"Clever" foresight What actually happens
I'll add a parameter "just in case" Every call site now has to pass or ignore it
I'll define an interface in case a second implementation appears The second implementation never arrives; the interface is dead weight
I'll add a config knob — we might want to tune it Nobody ever tunes it, but it still has to be supported
I'll extract an abstraction — we might need it The abstraction is awkward because it was built without the second concrete case

Any code you didn't write is zero bugs, zero tests, zero lines in CI, and zero review hours.


Example 1: "just-in-case" parameters

❌ Bad

def send_email(
    to: str,
    subject: str,
    body: str,
    cc: list | None = None,
    bcc: list | None = None,
    attachments: list | None = None,
    priority: str = "normal",
    encoding: str = "utf-8",
    reply_to: str | None = None,
    html: bool = False,
    template_engine: str | None = None,
) -> None:
    # 80% of these parameters were added "just in case" and are never used
    ...

✅ Good

def send_email(to: str, subject: str, body: str) -> None:
    ...

Need cc? Add it when you need it. Git keeps the history — nothing is lost.


Example 2: "flexible" abstractions with only one implementation

❌ Bad

from abc import ABC, abstractmethod

class StorageBackend(ABC):
    @abstractmethod
    def save(self, key: str, data: bytes) -> None: ...
    @abstractmethod
    def load(self, key: str) -> bytes: ...

class FileStorage(StorageBackend):
    def save(self, key, data): ...
    def load(self, key): ...

# And S3Storage / RedisStorage / DatabaseStorage never actually showed up

The interface was built for "in case we want S3." We didn't. But it still needs maintenance.

✅ Good

class FileStorage:
    def save(self, key: str, data: bytes) -> None: ...
    def load(self, key: str) -> bytes: ...

When S3 actually appears, that's when you extract the interface — modern IDE refactoring makes it a 5-minute job. Until then, simpler code.


Example 3: config knobs nobody touches

❌ Bad

class Config:
    DB_HOST: str
    DB_PORT: int
    DB_POOL_SIZE: int          # always 10
    DB_POOL_TIMEOUT: int       # always 30
    DB_RETRY_COUNT: int        # always 3
    DB_RETRY_BACKOFF: float    # always 1.5
    DB_CONNECT_TIMEOUT: int    # always 5
    ...

Twenty "settings," two of which actually get changed. The rest is noise in docs, environment files, and the developer's head.

✅ Good

class Config:
    DB_HOST: str
    DB_PORT: int
    # the rest stays hardcoded. If you ever need to change it, externalize it then.

Example 4: "extensibility" no one asked for

❌ Bad

def calculate_total(items, discount_strategy=None, tax_strategy=None,
                    rounding_strategy=None, currency_converter=None):
    ...

✅ Good

def calculate_total(items: list) -> float:
    subtotal = sum(i.price * i.qty for i in items)
    return subtotal * (1 + VAT_RATE)

When a second discount strategy actually appears, that's when you introduce the Strategy pattern. It's 15 minutes of work — but now informed by an actual second case.


When YAGNI does not apply

YAGNI is a powerful principle, but not dogma. Some things are cheaper to do upfront:

1. Security

Not "we'll add SQL-injection protection once we see an attack." Security goes in at the start.

2. Observability (logging, metrics)

Without logs, the first production bug costs 10× more to diagnose. Basic logging belongs in from day one.

3. Tests

Tests "for later" almost always means "never." At least the minimal set goes in alongside the code.

4. One-way-door architectural decisions

If the choice is cheap now but would require rewriting half the system later (sync vs async, relational vs document DB, monolith vs services), think it through upfront. Don't confuse this with speculative features.

Rule: YAGNI applies to features, not system qualities (security, reliability, observability, testability).


How to tell "needed now" from "needed later"

Ask yourself three questions:

  1. Is there a real user or use case right now that requires this?
  2. Will it be more expensive to add later than now? (Not "slightly more" — significantly more.)
  3. Am I confident I know what it should look like?

If the answer to any of them is no — don't add it.


YAGNI and its companion: "Worse is Better"

Richard Gabriel (1989) described a paradox: systems with a simpler, even "academically worse" design tend to outcompete systems with more "correct" but more complex designs. They are cheaper to maintain, easier to understand, faster to adapt.

YAGNI is the engineering form of the same principle: don't try to predict the future at the cost of the present.


Rules of thumb

  1. If you're adding a parameter/class/interface "just in case" — don't.
  2. You have git and an IDE with refactoring — adding it later will be easy.
  3. Exception for system qualities (security, observability, tests) — bake them in upfront.
  4. The rule of two: don't extract an abstraction until you have at least two concrete implementations. (Some go stricter and use the rule of three.)
  5. Every "option" is a branch of code, a branch of tests, and a branch of documentation. The cost isn't zero.

One-line summary

The best code is the code you didn't write. Only write what you need right now.