DRY — Don't Repeat Yourself
2.2.1· Coding › Design Principles
DRY KYA HAI?
Ek subtle trap: DRY knowledge ke baare mein hai, text ke baare mein nahi. Do lines of code jo dekhne mein identical lagtein hain lekin do alag-alag unrelated decisions represent karti hain — woh DRY violation nahi hai. (Iske baare mein mistakes section mein aur baat karenge — yeh #1 misunderstanding hai.)
DRY KYUN MATTER KARTA HAI?
Concretely, duplication teen axes par hurt karta hai:
| Axis | Duplicates ke saath kya gadbad hoti hai |
|---|---|
| Maintainability | Ek change → kai edits → ek bhool gaye → bug |
| Consistency | Copies time ke saath "drift" ho jaati hain |
| Readability | Reader samajh nahi sakta ki kya do copies deliberately identical hain |

DRY KAISE APPLY KARTE HAIN?
Worked Example 1 — Function extract karo (logic duplication)
WET (bad):
# order A
total_a = price_a + price_a * 0.18
# order B
total_b = price_b + price_b * 0.18Yeh bad kyun hai? Rule "total = price + 18% tax" do jagah exist karta hai. Tax change karo → 2 edits.
DRY (good):
TAX_RATE = 0.18 # Kyun? FACT ek baar exist karta hai
def with_tax(price): # Kyun? LOGIC ek baar exist karta hai
return price + price * TAX_RATE
total_a = with_tax(price_a)
total_b = with_tax(price_b)Yeh step kyun? Ab tax change karna = ek constant edit karo. .
Worked Example 2 — Single source of truth (data duplication)
WET: US states ki list frontend dropdown mein hard-coded aur backend validator mein bhi. Yeh bad kyun hai? Ek state add karo → dono jagah edit karni padegi, warna woh drift ho jayenge aur validation valid input ko reject kar dega.
DRY: List ek baar define karo (ek config/DB table), aur dono layers use read karein. Yeh step kyun? "Knowledge" (kaun se states exist hain) ka ek authoritative source hai.
Worked Example 3 — Loop instead of copy-paste
WET:
draw_button("Save")
draw_button("Cancel")
draw_button("Help")DRY:
for label in ["Save", "Cancel", "Help"]: # Kyun? structure ek baar express hota hai
draw_button(label)Yeh step kyun? Button draw karne ka procedure ab ek baar describe hota hai; data vary karta hai.
KAB DRY MAT KARO (the steel-man)
Active Recall
Recall DRY actually kya forbid karta hai — duplicate text ya duplicate knowledge?
Duplicate knowledge (ek logic/rule/fact jo ek se zyada authoritative jagah represent ho). Identical text jo unrelated decisions encode karta hai woh violation nahi hai.
Recall DRY argument mein forgetting-probability kyun matter karta hai?
Kyunki probability ki saare copies consistent rahein hai, jo exponentially decay karta hai. Zyada copies → missed edit ki bahut zyada chance → silent bug.
Recall Rule of Three kya hai?
Tab tak wait karo jab tak duplication ki teesri occurrence nahi dekh leti, phir abstract karo, taaki real pattern clear ho aur aap galat abstraction se bachen.
Recall Galat abstractions ke baare mein cheaper-than alternative quote do.
"Duplication is far cheaper than the wrong abstraction." — Sandi Metz.
Feynman: ek 12-saal ke bachche ko explain karo
Recall DRY simply explain karo
Imagine karo tumne apna ghar ka address 5 alag-alag notebooks mein likha. Agar tum ghar badlo, tumhe saari 5 dhundh ke fix karni padegi — aur ek miss karo toh koi kho jayega. Iske bajaye, apna address ek notebook mein likho aur sabko bolo "wahan dekho". Ab shift hone ka matlab sirf ek baar change karna hai. Code mein DRY ka matlab: har important fact ya rule ek jagah rakho, aur baaki sab us par point karein.
Connections
- Single Responsibility Principle — "one reason to change" true duplication ka test hai.
- WET — Write Everything Twice — woh anti-pattern jise DRY oppose karta hai.
- Rule of Three — DRYing ko delay kab karna chahiye.
- Single Source of Truth — DRY applied to data/config.
- Premature Abstraction / The Wrong Abstraction — DRY jab bahut zyada ho jaye.
- KISS Principle & YAGNI — DRY ko simplicity ke saath balance karo.
- Refactoring — Extract Method — logic duplication remove karne ka practical tool.