2.2.3Design Principles

YAGNI — You Aren't Gonna Need It

1,838 words8 min readdifficulty · medium2 backlinks

WHAT is YAGNI?

It is the discipline twin of Simplicity and KISS — Keep It Simple. While KISS says "make the thing you build simple," YAGNI says "don't build the thing at all (yet)."


WHY does YAGNI work? (derivation from first principles)

We can reason about it like an expected-value problem. Speculative feature has:

  • pp = probability the feature is ever actually needed as you imagined it.
  • CbuildC_{\text{build}} = cost to build it now.
  • CcarryC_{\text{carry}} = ongoing cost of carrying it (it must be read, tested, maintained, refactored around) even while unused.
  • SS = savings if you build now AND the guess is correct (you avoid building later).
  • ClaterC_{\text{later}} = cost to build it later when truly needed (often cheaper, because you'll then know the real requirement).

HOW to apply YAGNI (the workflow)

  1. Trigger check: Is there a concrete, present requirement (a user story, a ticket, a failing acceptance test)? If no → stop.
  2. Forecast-then-Verify: Write down why you think you'll need this. Predict when. Most predictions never come true — keep a log and see.
  3. Build the simplest thing that satisfies the current need only.
  4. Refactor when the second case arrives (see Rule of Three: abstract on the third occurrence, not the first).

Figure — YAGNI — You Aren't Gonna Need It

Common mistakes (Steel-manned)


Active recall

Recall Quick self-test (cover the answers)
  • What does the YAGNI inequality say must hold to justify speculative building? → pS>Cbuild+Ccarryp\,S > C_{\text{build}}+C_{\text{carry}}.
  • Why does CcarryC_{\text{carry}} make speculation expensive? → it is paid every day the unused code lives.
  • Does YAGNI tell you to skip input validation on a present endpoint? → No, that's a present requirement.
  • When do you finally add an abstraction? → On the third real occurrence (Rule of Three).
What does YAGNI stand for?
You Aren't Gonna Need It.
YAGNI core rule in one sentence?
Implement functionality only when you actually need it now, never just because you foresee needing it.
Inequality justifying speculative build now?
p·S > C_build + C_carry (expected benefit must exceed certain cost).
Why is C_carry the hidden killer?
Unused speculative code must still be read, tested, maintained and refactored around — a cost paid continuously even though it delivers nothing.
Why is building later often CHEAPER than building speculatively now?
Later you have the real requirement, so you avoid wrong guesses and rework (C_later < C_build for the actually-correct design).
Does YAGNI mean "no architecture / no validation"?
No — it forbids speculative future features, not design or non-functional needs required by the present.
YAGNI vs DRY difference?
DRY removes existing duplication; YAGNI prevents speculative future code. Abstracting too early to be DRY can violate YAGNI.
When should you finally extract an abstraction?
On the third real occurrence — the Rule of Three.

Recall Feynman: explain it to a 12-year-old

Imagine you're packing a small bag for a one-day school trip. A friend says "bring a tent, a stove, and snow boots — just in case we get lost in the mountains!" But you're going to a park for a few hours. Carrying all that heavy stuff every minute is real work, and you'll almost certainly never use it. YAGNI is just: only pack what today's trip needs. If a mountain trip ever happens for real, you'll pack for it then — and you'll know exactly what to bring.


Connections

  • KISS — Keep It Simple — build the present thing simply; YAGNI says maybe don't build it.
  • DRY — Don't Repeat Yourself — fight existing duplication, but not prematurely.
  • Rule of Three — the timing rule YAGNI hands off to for abstraction.
  • Premature Optimization — same disease (speculation), performance flavor.
  • Extreme Programming — the methodology YAGNI comes from.
  • Technical Debt — speculative code is pre-paid debt with no asset.

Concept Map

warns against

analyzed by

yields

small so

paid for sure in

weakens case for

usually fails so

discipline twin of

complements

applied via

abstract on third case

YAGNI: don't build until needed

Speculative feature

Expected value model

p probability guess right

C_carry paid daily

C_later often cheaper

p·S > C_build + C_carry

KISS — keep it simple

Simplicity

Rule of Three

Workflow: trigger check then build simplest

Hinglish (regional understanding)

Intuition Hinglish mein samjho

YAGNI ka matlab simple hai: "You Aren't Gonna Need It" — yaani jab tak abhi, is waqt, kisi feature ki real zaroorat na ho, tab tak usse mat banao. Hum log coding karte time aksar sochte hain "abhi config option add kar deta hoon, kal kaam aayega", ya "interface generic bana deta hoon, future mein Mongo, SQL, S3 sab support karna padega". Lekin sach yeh hai ki future ka tumhara guess zyada tar galat nikalta hai — requirement badal jaati hai.

Ise expected value se samjho: speculative code banane ka benefit tabhi milta hai jab guess sahi ho (probability p, savings S), par cost toh hamesha deni padti hai — banane ki cost plus carry cost (har din us code ko padhna, test karna, maintain karna). Formula banta hai: build karo tabhi jab pS>Cbuild+Ccarryp \cdot S > C_{build} + C_{carry}. Real life mein p chhota hota hai aur carry cost daily lagti hai, isliye yeh inequality usually fail karti hai — matlab abhi mat banao, defer karo.

Ek important confusion clear karo: YAGNI ka matlab "kuch bhi proper mat banao" NAHI hai. Input validation, security, error handling jo abhi ki requirement hain — woh toh banane hi hain. YAGNI sirf speculative future features ko rokta hai, na ki present ki genuine needs ko. Aur jab teesri baar wahi cheez sach mein repeat ho (Rule of Three), tab abstraction banao — pehli baar mein nahi.

Bottom line: ek guess koi ticket nahi hai. Jab tak concrete present requirement na ho, code mat likho. Isse codebase chhota, simple, easy-to-change rehta hai — aur jab real zaroorat aati hai to tum behtar, sahi design banate ho.

Go deeper — visual, from zero

Test yourself — Design Principles

Connections