DRY Principle (Don't Repeat Yourself)
A software development principle stating that every piece of knowledge should have a single, authoritative representation in a system.
Definition
DRY was formulated by Andy Hunt and Dave Thomas in "The Pragmatic Programmer." It's not just about avoiding copy-paste code — it's about avoiding duplication of knowledge and intent. When logic is duplicated, changes must be made in multiple places, increasing the risk of bugs. Common techniques to achieve DRY: functions/methods, classes, modules, templates, and code generation. However, premature abstraction can be worse than some duplication — balance is key.
Why It Matters
DRY violations (WET code - "Write Everything Twice") make maintenance expensive and error-prone. Studies show that duplicated code has 50% higher defect rates because fixes are often applied inconsistently. Code review tools flag similar code blocks to identify potential DRY violations.
Example
WET code: validation logic for email format is copied into 5 different form handlers. DRY solution: extract validateEmail(email) function used by all handlers.