Category: Principles
Type: Software Development Principle
Origin: Software Engineering, 1990s / The Pragmatic Programmer, 1999
Also known as: DRY, Don’t Repeat Yourself, Single Source of Truth
Type: Software Development Principle
Origin: Software Engineering, 1990s / The Pragmatic Programmer, 1999
Also known as: DRY, Don’t Repeat Yourself, Single Source of Truth
Quick Answer — The DRY Principle (Don’t Repeat Yourself) states that every piece of knowledge must have a single, unambiguous representation within a system. First articulated in “The Pragmatic Programmer” by Andy Hunt and Dave Thomas in 1999, DRY has become one of the most influential principles in software development. The principle extends beyond code to include documentation, database schemas, tests, and any other system artifact. When information is duplicated, changes require multiple updates, increasing the risk of inconsistencies and bugs.
What is the DRY Principle?
The DRY Principle is a software development philosophy that advocates for eliminating duplication in code, documentation, and other system artifacts. The core idea is deceptively simple: any piece of knowledge—be it a business rule, a calculation, a data structure, or a user interface element—should exist in exactly one place. When the same information appears multiple times, developers must remember to update every instance, creating ongoing maintenance burden and introducing opportunities for errors.“Duplication is the primary enemy of a well-designed system.” — Andy Hunt and Dave Thomas, The Pragmatic ProgrammerThe principle gained widespread recognition through “The Pragmatic Programmer” (1999), a influential book that established many best practices for software development. However, the underlying idea predates the book—programmers have long recognized that duplicating logic creates maintenance nightmares. What Hunt and Thomas did was articulate the principle clearly and give it a memorable name. DRY applies to many forms of duplication. Code duplication occurs when the same logic appears in multiple places. Documentation duplication occurs when the same information is explained in multiple documents. Data duplication happens when the same data is stored in multiple locations. Schema duplication arises when database structures mirror each other. The principle asks developers to identify the single authoritative source for each piece of knowledge and build systems that reference this source.
DRY Principle in 3 Depths
- Beginner: When copying and pasting code, stop and ask: “Can I extract this into a function or variable that can be reused?” Even small duplications compound over time.
- Practitioner: Apply DRY at multiple levels—functions, classes, modules, and services. Use techniques like inheritance, composition, and configuration management to centralize knowledge.
- Advanced: Recognize that over-aggressive DRY can create inappropriate coupling. Sometimes duplication is preferable to the wrong abstraction. Balance DRY with the YAGNI and KISS principles.
Origin
The DRY Principle was formally introduced in “The Pragmatic Programmer: From Journeyman to Master” (1999) by Andy Hunt and Dave Thomas. The book, which became a cornerstone of software development literature, described DRY as a core principle for building maintainable software. Hunt and Thomas argued that duplication leads to systems that are harder to understand, modify, and extend. The principle built on earlier ideas from software engineering. The concept of “single source of truth” in database design predates DRY, as do practices like defining constants in one place and referencing them throughout code. What made DRY influential was its broad application across all forms of knowledge duplication, not just in code but in documentation, processes, and organizational structures. The timing of DRY’s popularization was significant. The late 1990s saw the growth of large-scale software systems, and developers were increasingly facing the maintenance challenges of duplicated codebases. DRY provided a clear heuristic for making design decisions that would pay dividends over the lifetime of a system. Today, DRY remains a foundational principle taught in computer science programs and followed by professional development teams worldwide.Key Points
Single Source of Truth
Each piece of information should exist in exactly one place. When information must change, there should be only one location to update, eliminating the risk of inconsistencies.
Reduces Maintenance Burden
Duplicated code requires duplicated maintenance. Bug fixes, security updates, and feature changes must be applied consistently across every copy, increasing workload and risk.
Improves Consistency
When knowledge exists in one place, all consumers reference the same authoritative source, ensuring consistent behavior throughout the system.
Applications
Code Refactoring
Extract duplicated logic into reusable functions, classes, or modules. Use inheritance or composition to share behavior without duplicating implementation.
Configuration Management
Store configuration values in centralized files or environment variables. Avoid hardcoding values that might change across environments.
Database Design
Normalize database schemas to eliminate redundant data storage. Use foreign keys and joins rather than duplicating data across tables.
Documentation
Write documentation that explains concepts once and references that explanation throughout. Avoid restating the same information in multiple places.
Case Study
The Ruby on Rails framework famously embodies the DRY Principle. Rails introduces the concept of “convention over configuration,” where common patterns are defined once and automatically applied throughout applications. For example, model definitions in Rails automatically generate database access methods—the same logic is not duplicated in multiple places. When Rails developers need to change how models interact with databases, they make the change in one place, and it propagates throughout the application. This approach has made Rails applications notoriously easy to maintain and modify, with developers reporting significantly less duplicated code compared to frameworks that don’t emphasize DRY as heavily.Boundaries and Failure Modes
The DRY Principle, while essential, has important limitations. First, overzealous DRY can create inappropriate abstractions. Forcing unrelated concepts into shared code can create tight coupling, making both the original code and the “refactored” version harder to maintain. Second, premature abstraction is risky. DRY should guide refactoring of proven duplication, not inspire speculative generalization. Creating abstractions for code that might someday be reused often leads to complex, hard-to-understand systems. Third, some duplication is acceptable—or even necessary. Code that appears similar may have different reasons for existing, and forcing them together creates a false unity. Test code often duplicates structure from production code for good reason: tests must be independently maintainable.Common Misconceptions
DRY means never copy code
DRY means never copy code
DRY is about avoiding duplication of knowledge, not literal code. Sometimes the clearest solution involves repetition of syntax while the underlying knowledge remains single-sourced.
DRY always improves maintainability
DRY always improves maintainability
Incorrect abstraction can make maintenance harder. Before extracting duplication, ensure the abstraction accurately represents the shared concept.
DRY applies only to code
DRY applies only to code
The principle extends to documentation, database schemas, API designs, and organizational processes. Any repeated knowledge is a candidate for DRY treatment.
Related Concepts
KISS Principle
DRY often simplifies code, making it easier to understand. KISS complements DRY by emphasizing that simplicity should guide how duplication is eliminated.
YAGNI Principle
Don’t build reusable components until you need them. YAGNI prevents premature abstraction that violates DRY’s spirit.
Single Source of Truth
A related database concept where data should be stored in one place. DRY is the application of this idea to all knowledge in a system.
Code Refactoring
The practice of improving code structure, often by applying DRY. Refactoring is the operational activity that implements DRY.
Occam's Razor
Both principles favor simplicity. DRY eliminates duplication, while Occam’s Razor eliminates unnecessary assumptions.