Definition: DRY (Don't Repeat Yourself) is a software development principle stating that every piece of knowledge should have a single authoritative representation within a system.
— Source: NERVICO, Software Development Consultancy
What is DRY
DRY was formulated by Andy Hunt and Dave Thomas in their book "The Pragmatic Programmer" (1999). The central idea is simple: if knowledge is duplicated, eventually the copies will diverge and you'll have bugs.
But beware: DRY isn't just about duplicated code. It's about duplicated knowledge. Two identical lines of code may represent different knowledge (and that's fine). Two different lines may represent the same knowledge (and that's a problem).
Why Duplication is Dangerous
- Forgotten changes: You change one copy but forget the others. Bug guaranteed.
- Inconsistency: Different parts of the system behave differently for the same thing.
- Multiplied maintenance: Each copy must be maintained, tested, documented.
- Difficult comprehension: Which is the "official" version? Why are there differences?
Types of Duplication
Imposed Duplication
The environment forces you to duplicate. APIs that require the same data in multiple places, frameworks that need redundant configuration. Minimise impact by isolating duplication and generating code where possible.
Accidental Duplication
You don't realise it already exists. The team doesn't know all the code, or searching is difficult. Solution: better documentation, code reviews, communication.
Lazy Duplication
It's easier to copy and paste than to think. We've all done it. The solution is discipline and code reviews that catch it.
"False" Duplication
Code that looks duplicated but represents different concepts. Two functions that calculate "price" but one is for inventory and another for billing. Don't unify this — they're different knowledge that will evolve independently.
How to Apply DRY
Extract Functions
If you're copying a block of code, it's probably a function waiting to be born. Give it a name that explains what it does, not how it does it.
Use Constants
Repeated magic values (URLs, configurations, messages) should be in a single place. If the value changes, you change one line.
Create Abstractions
Repeated code patterns can be base classes, mixins, decorators or higher-order functions.
Generate Code
If duplication is unavoidable (schemas, DTOs, configurations), generate the code from a single source of truth.
The Mistake of Applying DRY Too Early
Premature DRY is as bad as duplication. Before extracting common code, you need to see the pattern at least three times (the "Rule of Three").
If you unify code too early, you may create incorrect abstractions that are then difficult to undo. Sometimes it's better to have duplicated code for a while until you understand what they really have in common.
DRY vs WET
WET (Write Everything Twice, or "We Enjoy Typing") is the opposite of DRY. But there's also the concept of "WET when appropriate" — duplicate when:
- Code looks the same but represents different concepts
- The abstraction would be more complex than the duplication
- Cases will evolve independently
- Duplication is small and localised
Practical Examples
Bad: Duplicated Validation
You validate email on the frontend, on the backend and in the database with different regex. When the requirement changes, you forget to update one.
Solution: A single shared or generated validation function.
Bad: Scattered Configuration
The API URL is hardcoded in 15 different files.
Solution: A constant or environment variable in one place.
Good: Similar but Different Code
calculateSalePrice() and calculatePurchasePrice() have similar logic but different business rules. Keeping them separate allows them to evolve independently.
