2.2.1 · HinglishDesign Principles

DRY — Don't Repeat Yourself

1,714 words8 min readRead in English

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
Figure — DRY — Don't Repeat Yourself

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.18

Yeh 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.

DRY stands for?
Don't Repeat Yourself.
DRY ka core rule (Pragmatic Programmer definition)?
Har piece of knowledge ka system mein ek single, unambiguous, authoritative representation hona chahiye.
DRY duplicate text ko target karta hai ya duplicate knowledge ko?
Duplicate knowledge (logic/rules/facts) ko, coincidentally-identical text ko nahi.
Edit cost vs number of copies n?
Linearly badhta hai: C = n·c.
Probability ki saare n copies consistent rahein agar slip-prob p ho?
(1−p)^n — exponentially decay karta hai.
Accidental/coincidental duplication kya hai?
Code jo dekhne mein same lagta hai lekin unrelated decisions encode karta hai jo alag reasons se change hote hain; inhe merge NAHI karna chahiye.
Yeh decide karne ka test ki duplication real hai ya nahi?
"Kya yeh ek hi reason se saath-saath change hote hain?" Haan → DRY karo; Nahi → alag rakho.
Rule of Three?
Duplication ko teesri occurrence tak tolerate karo, phir abstraction extract karo.
Abstractions ke baare mein Sandi Metz kya kehti hain?
"Duplication is far cheaper than the wrong abstraction."
DRY ka opposite?
WET — Write Everything Twice.
Data/config ke liye DRY ko kya kehte hain?
Single Source of Truth.
Over-DRYing ka main risk?
Premature/wrong abstraction with too many flags, original duplication se bhi zyada hard to read.

Concept Map

means

about

opposite of

causes

raises

lowers

hurts

hurts

applied by

references

via

DRY Principle

Single Authoritative Representation

Knowledge not Text

WET Duplication

Multiple Copies n

Edit Cost = n·c linear

Consistency 1-p to the n

Maintainability

Copies Drift Apart

Extract to Single Home

Function Constant Class Template