BolEnglishLearn · Play · Win
Login

♻️ Software Design Principles · Core Principles

♻️

DRY - Don't Repeat Yourself

Design PrincipleCoreModule 2

Avoid unnecessary duplication of knowledge or business rules.

If the same rule exists in many places, a future change can easily become inconsistent.

💡 In practice

Put age eligibility validation in one shared rule instead of copying it into five forms.

🖼️ CONCEPT PICTURE
A picture to help remember DRY - Don't Repeat Yourself
Put age eligibility validation in one shared rule instead of copying it into five forms.

💡 Like one master recipe card everyone cooks from — not five handwritten copies that slowly drift apart.

💡 Learn🛠️ Apply🎯 Check

💡 Why it matters

When the same rule is copied into many places, a change must be made everywhere at once, and one missed copy creates a bug. Keeping each rule in one place means you change it once and the whole system stays consistent. This is really about one source of truth, not just avoiding similar-looking code.

🛠️ How to achieve it

1
Find the single truth
When the same rule appears twice, move it into one shared function or value that everyone uses.
2
Share rules, not coincidences
Only combine code that means the same thing. Two lines that just look alike today may need to change separately tomorrow.
3
Name the rule clearly
Give the shared piece a name that explains the rule, so callers know what they are reusing.
4
Centralize constants
Keep magic numbers and strings, like a minimum age, in one named place instead of scattering them.

⚠️ Signs of trouble

  • •The same rule is copy-pasted across several files.
  • •A bug fix has to be applied in many places.
  • •Two copies of a rule have quietly drifted out of sync.
  • •The same magic number appears all over the code.
⚠️ Common mistake: Copy-pasting a rule to save time, then forgetting one copy when the rule changes. Keep each rule in one named place that every caller shares.

🎯 Quick check

Score: 0/3

Three questions to test the idea.

  1. 1. What is DRY really about?

    Answer: Keeping each piece of knowledge in one place

    DRY means one source of truth for each rule, so a change is made once and stays consistent.

  2. 2. When should you NOT merge two similar-looking blocks of code?

    Answer: When they only look alike but may change for different reasons

    Code that is similar by coincidence can diverge later, so forcing it together causes more harm than good.

  3. 3. The minimum age 18 appears in five files. What is the DRY fix?

    Answer: Define it once as a named constant everyone uses

    A single named constant means changing the age once updates every place at once.

🔑 Key terms

Single source of truth
One place that holds a fact or rule for the whole system.
Duplication
The same knowledge repeated in more than one spot.
Magic number
An unexplained raw value buried in code, like 18.
Refactor
Improving the structure of code without changing what it does.

✅ Review checklist

📊 Your progress

0%
Mark this topic learned to track it.
Saved in this browser.
🚀 Got it?
Mark it learned and move to the next topic.
Next topic →