What if fixing one bug could fix it everywhere instantly?
Why DRY (Don't Repeat Yourself) in LLD? - Purpose & Use Cases
Imagine you are building a system where you write the same code or logic in many places to handle user authentication. Every time you need to change the login process, you must find and update all those places manually.
This manual approach is slow and risky. You might miss some places, causing bugs. It becomes hard to maintain and update the system. The repeated code wastes time and makes the system fragile.
DRY means writing the logic once and reusing it everywhere. This way, you update the code in one place, and all parts of the system benefit. It makes the system cleaner, easier to maintain, and less error-prone.
if user.is_authenticated: show_dashboard() # repeated in many files
def check_auth(user): return user.is_authenticated if check_auth(user): show_dashboard()
DRY enables building systems that are easier to change, less buggy, and faster to develop.
Think of a website where the password rules are checked in many places. With DRY, you write the rules once and reuse them, so when rules change, you fix it in one place only.
Repeating code causes bugs and wastes time.
DRY means write once, use everywhere.
It makes systems easier to maintain and update.
Practice
DRY principle in system design?Solution
Step 1: Understand the meaning of DRY
DRY stands for "Don't Repeat Yourself," which means avoiding repeated code or design.Step 2: Identify the goal of DRY
The goal is to keep systems simple, maintainable, and reduce bugs by reusing components.Final Answer:
To avoid repeating code or design to keep systems simple -> Option AQuick Check:
DRY = Avoid repetition [OK]
- Thinking DRY means writing more code
- Believing duplication speeds development
- Confusing DRY with writing multiple versions
Solution
Step 1: Identify DRY best practice
DRY encourages reuse by creating components or modules that can be used multiple times.Step 2: Evaluate options
Copy-pasting or writing separate code repeats work, which breaks DRY. Ignoring reuse wastes effort.Final Answer:
Creating reusable components or modules -> Option AQuick Check:
Reuse = DRY [OK]
- Copy-pasting code instead of reusing
- Writing separate code for similar tasks
- Ignoring reuse to save time
function calculateTax(amount) {
return amount * 0.1;
}
function calculateTotal(amount) {
return amount + calculateTax(amount);
}
print(calculateTotal(100));
What is the output?Solution
Step 1: Calculate tax for amount 100
Tax = 100 * 0.1 = 10Step 2: Calculate total using tax
Total = 100 + 10 = 110Final Answer:
110 -> Option CQuick Check:
100 + 10 = 110 [OK]
- Forgetting to add tax to amount
- Confusing tax amount with total
- Assuming syntax error without checking code
function sendEmailToUser1() {
// send email logic
}
function sendEmailToUser2() {
// send email logic
}
What is the best fix to follow DRY?Solution
Step 1: Identify repeated code
Both functions have the same email sending logic repeated.Step 2: Refactor to reusable function
Create one function with a parameter for user to reuse the logic.Final Answer:
Create one function sendEmail(user) and reuse it -> Option BQuick Check:
Refactor repeated code = DRY [OK]
- Leaving duplicate functions unchanged
- Copy-pasting code more times
- Removing needed functionality
Solution
Step 1: Identify repeated authentication logic
Multiple services need the same authentication, so repeating code breaks DRY.Step 2: Use a shared authentication service
Centralizing authentication logic in one service allows reuse and easier maintenance.Final Answer:
Create a shared authentication service used by all services -> Option DQuick Check:
Shared service = DRY in system design [OK]
- Duplicating authentication code in each service
- Skipping authentication for simplicity
- Copy-pasting code instead of sharing
Ready to prove it?
Pass the test and this pattern moves unknown
