SOLID & Design Patterns Interview Question #132
What is the DRY principle and when can it be misused?
Anti-Patterns, Refactoring, DI Lifetimes & Senior Design Scenarios Senior Advanced
Quick Interview Answer
DRY means Don't Repeat Yourself and is primarily about avoiding duplicated knowledge or business rules. When the same rule changes, it should ideally have one authoritative representation.
Detailed Explanation
DRY means Don't Repeat Yourself and is primarily about avoiding duplicated knowledge or business rules. When the same rule changes, it should ideally have one authoritative representation.
DRY can be misused by combining code that merely looks similar but represents different concepts. Removing every repeated line can create inappropriate coupling. Duplication can sometimes be cheaper than the wrong abstraction.
Code Example
// Same business rule in several places:
// good candidate for one source of truth.
// Two similar-looking workflows with
// different reasons to change:
// do not merge automatically.