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.
This diagram shows two places with repeated code replaced by calls to a single reusable function, centralizing the logic.
### 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)
DRY principle in system design?function calculateTax(amount) {
return amount * 0.1;
}
function calculateTotal(amount) {
return amount + calculateTax(amount);
}
print(calculateTotal(100));
What is the output?function sendEmailToUser1() {
// send email logic
}
function sendEmailToUser2() {
// send email logic
}
What is the best fix to follow DRY?Pass the test and this pattern moves unknown