0
0
LLDsystem_design~7 mins

DRY (Don't Repeat Yourself) in LLD - System Design Guide

Choose your learning style9 modes available
Problem Statement
When the same code or logic is copied in multiple places, it becomes hard to maintain. Fixing a bug or updating a feature requires changes in many spots, increasing the chance of errors and inconsistencies.
Solution
DRY solves this by ensuring every piece of knowledge or logic exists in only one place. Instead of copying code, you create reusable functions or classes and call them wherever needed, so updates happen in one spot only.
Architecture
Repeated
Code Block 1
Single Function
(Reusable logic called by both places)

This diagram shows two places with repeated code replaced by calls to a single reusable function, centralizing the logic.

Trade-offs
✓ Pros
Reduces bugs by having a single source of truth for logic.
Simplifies maintenance since changes happen in one place.
Improves code readability by avoiding clutter from repeated code.
✗ Cons
May introduce extra function calls, slightly impacting performance in critical paths.
Over-abstraction can make code harder to follow if taken too far.
Requires upfront design effort to identify reusable parts.
Use DRY when the same logic or code appears in two or more places, especially in medium to large codebases where maintenance cost is high.
Avoid DRY in very small scripts or one-off code where abstraction adds unnecessary complexity and overhead.
Real World Examples
Amazon
Amazon uses DRY principles in their payment processing code to ensure all payment validation logic is centralized, reducing errors and speeding up updates.
Netflix
Netflix applies DRY in their streaming service code to reuse authentication checks across multiple microservices, ensuring consistent security.
Stripe
Stripe uses DRY to centralize billing calculations, so all products and services use the same pricing logic, avoiding discrepancies.
Code Example
The before code repeats the multiplication logic in two functions. The after code extracts multiplication into a single function 'multiply' and reuses it, following DRY.
LLD
### Before applying DRY (repeated code)
def calculate_area_rectangle(width, height):
    return width * height

def calculate_area_square(side):
    return side * side

# Both functions repeat multiplication logic

### After applying DRY (reusable function)
def multiply(a, b):
    return a * b

def calculate_area_rectangle(width, height):
    return multiply(width, height)

def calculate_area_square(side):
    return multiply(side, side)
OutputSuccess
Alternatives
WET (Write Everything Twice)
WET allows code duplication without abstraction, leading to repeated logic in multiple places.
Use when: Choose WET only for very small or experimental projects where speed of writing is more important than maintainability.
Copy-Paste Programming
Directly copying code blocks instead of abstracting them into reusable units.
Use when: Use only in quick prototyping or throwaway scripts where code reuse is not a concern.
Summary
DRY prevents bugs and eases maintenance by centralizing repeated logic into one place.
It encourages reusable functions or classes instead of copying code.
Applying DRY requires balancing abstraction with code clarity.