DRY
Also known as: Don't Repeat Yourself
Hunt and Thomas' principle: every piece of knowledge should have one authoritative representation.
DRY (Don't Repeat Yourself) was articulated by Andy Hunt and Dave Thomas in The Pragmatic Programmer (1999) as the principle that 'every piece of knowledge must have a single, unambiguous, authoritative representation within a system.' The principle is often summarized as avoiding code duplication, but Hunt and Thomas's original meaning was broader and subtler: knowledge in a system, whether expressed in code, configuration, documentation, schemas, or build scripts, should have one canonical source from which other representations are derived if needed. The pragmatic intent is preventing the divergence and inconsistency that arises when the 'same' fact is expressed in multiple places that must be kept in sync manually. Sandi Metz's 'duplication is far cheaper than the wrong abstraction' has become a widely-cited corrective: the antidote to duplication is the right abstraction, not any abstraction, and premature abstraction can be more costly than the duplication it removes. DRY is thus best understood as guidance against accidental duplication of shared knowledge, not as a prohibition on superficially similar code.
Core components
- Single authoritative representation of knowledge
- Distinction between knowledge duplication and superficially similar code
- Application across code, configuration, documentation, schemas, build scripts
- Code generation and template-based derivation as DRY-supporting techniques
- Sandi Metz's corrective: 'duplication is far cheaper than the wrong abstraction'
- Connection to abstraction quality rather than abstraction quantity
Primary use case
Code review and refactoring heuristic; teaching tool in software engineering education; widely-cited reference in clean-code and software-craftsmanship communities; basis for many code-quality and static-analysis rules.
Common criticisms
- Often misapplied as 'don't have any code that looks similar' rather than the original 'don't duplicate knowledge', leading to premature and incorrect abstractions
- Sandi Metz, Dan Abramov, and others have argued the wrong abstraction is more costly than duplication and recommend duplicating until the right abstraction reveals itself
- cross-cutting abstractions (DRY at all costs) often produce coupling that makes systems harder to change
- the 'rule of three' (Hunt and Thomas's later refinement: extract abstraction only after the third occurrence) is often forgotten
- DRY can mask domain differences that should be preserved, particularly when applied across bounded contexts
- mechanical de-duplication tools enforce DRY at the syntactic level rather than the knowledge level Hunt and Thomas intended.
Lineage
- Siblings
- KISS, YAGNI, SOLID Principles