2.2.3 · Coding › Design Principles
Koi feature tab tak mat banao jab tak abhi, is waqt, uski zaroorat na ho. Speculative "shayad baad mein chahiye hoga" wala code ek future ke against bet hai — aur tum almost hamesha haarte ho, kyunki jo future tum sochte ho woh kabhi waisa nahi aata.
YAGNI ("You Aren't Gonna Need It") ek Extreme Programming principle hai jisme kaha gaya hai ki programmer ko tab tak koi functionality add nahi karni chahiye jab tak woh zaroori na samjhi jaye . Ise Ron Jeffries ne coin kiya / XP community ne popular banaya.
Slogan: "Cheezein tab implement karo jab tumhe actually zaroorat ho, kabhi bhi tab nahi jab tum sirf yeh socho ki zaroorat padegi."
Yeh Simplicity aur KISS — Keep It Simple ka discipline twin hai. KISS kehta hai "jo cheez banao usse simple rakho," jabki YAGNI kehta hai "woh cheez banao hi mat (abhi)."
Hum ise ek expected-value problem ki tarah soch sakte hain. Speculative feature mein hota hai:
p = probability ki feature kabhi actually zaroorat padegi jaisi tumne sochi thi .
C build = ise abhi banane ki cost.
C carry = ise carry karne ki ongoing cost (ise unused rehte hue bhi padha, test kiya, maintain kiya, aur refactor ke around rakhna padta hai).
S = savings agar tum abhi banao AUR guess sahi nikle (baad mein banana avoid ho jata hai).
C later = ise baad mein banane ki cost jab sach mein zaroorat ho (aksar sasti hoti hai, kyunki tab real requirement pata hoti hai).
Intuition YAGNI almost hamesha KYUN jeetta hai
Real software mein p chhota hota hai (requirements drift karti hain), C carry > 0 har roz pay hoti hai, aur C later aksar C build se kam hoti hai kyunki future-you ke paas guesses ki jagah real requirements hoti hain. Toh inequality usually fail hoti hai → mat banao ise.
Trigger check: Kya koi concrete, present requirement hai (koi user story, koi ticket, koi failing acceptance test)? Agar nahi → ruko.
Forecast-then-Verify: Likho ki kyun tumhe lagta hai yeh chahiye hoga. Predict karo kab. Zyaatar predictions kabhi sach nahi hoti — log rakho aur dekho.
Sabse simple cheez banao jo sirf current need ko satisfy kare.
Refactor karo jab doosra case aaye (dekho Rule of Three : teesri occurrence pe abstract karo, pehli pe nahi).
Worked example Worked example 1 — "configurable" database
Story: User records PostgreSQL mein save karo.
Tempting speculative code:
class UserRepo :
def __init__ (self, backend = "postgres" ):
self .backend = backend # "mongo", "mysql", "s3" ... someday!
Yeh sahi kyun lagta hai: "Agar baad mein database switch karna pade? Main humein ek rewrite bachaunga."
YAGNI fix:
class UserRepo :
def __init__ (self, pg_conn):
self .pg = pg_conn
Yeh step kyun? Koi ticket nahi hai jo Mongo maang raha ho. p bahut chhota hai, aur abstraction layer hamesha ke liye padhni aur maintain karni padti hai (C carry ). Agar sach mein switch aaya, tab refactor karoge — aur tumhe real interface pata hogi jo chahiye.
Worked example Worked example 2 — premature generic parameters
Story: Welcome email bhejo.
Speculative: send(template, locale, channel="email", retries=5, priority="normal", schedule=None).
Kyun sahi lagta hai: "Notifications ko obviously SMS, scheduling, retries eventually chahiye hogi."
Fix: send_welcome_email(user).
Yeh step kyun? Har extra parameter untested surface area hai. Jab SMS sach mein aaye, tab tum Notifier extract karna — tab , dono real cases se informed hokar (Rule of Three).
Worked example Worked example 3 — YAGNI ka matlab "sab skip karo" nahi hai
Story: Public API endpoint jo untrusted input le raha hai.
Input validation, auth, error handling abhi zaroori hain — yeh ek present requirement protect karte hain (security/correctness). YAGNI yeh nahi hatata.
Yeh step kyun? YAGNI speculative future features ko target karta hai, currently-required non-functional needs ko nahi. Donon ko confuse karna sabse khatarnak misread hai.
Common mistake "Hook abhi add karna basically free hai."
Kyun sahi lagta hai: Empty extension point likhne mein 5 minute lagte hain — sasta!
Flaw: Cost woh 5 minute nahi hai; woh hai C carry — har future reader ko ise samajhna padega, har refactor ko ise preserve karna padega, har test ko ise cover karna padega, aur yeh designs ko ek imaginary requirement ke around constrain karta hai.
Fix: Cost tabhi pay karo jab real requirement aaye.
Common mistake "YAGNI ka matlab hai kabhi plan mat karo / koi architecture mat karo."
Kyun sahi lagta hai: Sunne mein lagta hai "bas minimum hack karo."
Flaw: YAGNI speculative features/abstractions mana karta hai, present needs ke liye achhe design, tests, ya clean boundaries ko nahi. Aaj ki requirements serve karne wali architecture speculative nahi hai.
Fix: Abhi zaroori (achhe se karo) aur shayad baad mein zaroori (defer karo) mein antar rakho.
Common mistake YAGNI aur DRY ko confuse karna.
Kyun sahi lagta hai: Dono bure code se ladte hain.
Flaw: DRY existing duplication hatata hai; YAGNI future-speculative code ko rokta hai. DRY bahut jaldi apply karna (pehli duplication par abstract karna) actually YAGNI violate karta hai — Rule of Three ka wait karo.
Recall Quick self-test (answers cover karo)
YAGNI inequality kya kehti hai jo speculative building justify kare? → p S > C build + C carry .
C carry speculation ko expensive kyun banati hai? → yeh har roz pay hoti hai jab tak unused code zinda hai.
Kya YAGNI kehta hai present endpoint par input validation skip karo? → Nahi, woh present requirement hai.
Abstraction finally kab add karte hain? → Teesri real occurrence par (Rule of Three).
What does YAGNI stand for? You Aren't Gonna Need It.
YAGNI core rule in one sentence? Functionality tabhi implement karo jab abhi actually zaroorat ho, sirf isliye nahi ki shayad baad mein padegi.
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 ko phir bhi padha, test kiya, maintain kiya aur refactor kiya jata hai — ek cost jo continuously pay hoti hai chahe kuch deliver na kare.
Why is building later often CHEAPER than building speculatively now? Baad mein real requirement hoti hai, toh galat guesses aur rework se bachte hain (C_later < C_build for the actually-correct design).
Does YAGNI mean "no architecture / no validation"? Nahi — yeh speculative future features mana karta hai, present ke liye zaroori design ya non-functional needs ko nahi.
YAGNI vs DRY difference? DRY existing duplication hatata hai; YAGNI speculative future code rokta hai. Bahut jaldi abstract karna DRY ke liye YAGNI violate kar sakta hai.
When should you finally extract an abstraction? Teesri real occurrence par — Rule of Three.
Recall Feynman: ek 12-saal ke bachche ko samjhao
Soch karo tum ek ek-din ki school trip ke liye ek chhota bag pack kar rahe ho. Ek dost kehta hai "tent, stove, aur snow boots le jao — bas agar hum pahadon mein kho jaayen!" Lekin tum kuch ghanton ke liye ek park ja rahe ho. Itna bhaari saman har minute uthana real kaam hai, aur tum ise almost certainly kabhi use nahi karoge. YAGNI bas yahi hai: sirf woh pack karo jo aaj ki trip ko chahiye. Agar kabhi sach mein pahad ki trip hogi, tab uske liye pack karna — aur tab tumhe exactly pata hoga kya laana hai.
YAGNI = "Yeah, A Guess, Not an Instruction." Future ke baare mein guess koi ticket nahi hoti. Koi ticket nahi → koi code nahi.
KISS — Keep It Simple — present cheez ko simply banao; YAGNI kehta hai shayad banao hi mat.
DRY — Don't Repeat Yourself — existing duplication se lado, lekin prematurely nahi.
Rule of Three — timing rule jis par YAGNI abstraction ke liye hand off karta hai.
Premature Optimization — same bimari (speculation), performance flavor.
Extreme Programming — woh methodology jahan se YAGNI aata hai.
Technical Debt — speculative code pre-paid debt hai jisme koi asset nahi.
YAGNI: don't build until needed
p probability guess right
Workflow: trigger check then build simplest