DRY
Don't Repeat Yourself
In short
DRY is a software design principle stating that every piece of knowledge or logic should have one authoritative representation instead of being duplicated.
What is the DRY principle?
DRY stands for Don't Repeat Yourself, a principle introduced in the 1999 book The Pragmatic Programmer by Andy Hunt and Dave Thomas. Its original wording is that every piece of knowledge must have a single, unambiguous, authoritative representation within a system. In practice, that means a business rule, a calculation, or a configuration value should be defined in one place and reused everywhere else.
When the same logic is copied into several places, every future change must be made in all of them, and it is easy to miss one, which leads to bugs where parts of the application disagree. Developers apply DRY by extracting shared code into functions, modules, constants, or components, and by generating repeated artifacts, such as API clients or database schemas, from a single source.
An analogy is a company phone number printed separately on a hundred flyers versus one number on a website that every flyer points to: when the number changes, only one place needs updating. DRY applies not only to code but also to documentation, tests, database schemas, and build configuration.
A common misunderstanding is that DRY means removing every piece of similar-looking code. Two blocks can look alike today but represent different business rules that will change for different reasons, and merging them creates a tangled, over-general abstraction that is harder to change than the duplication was. A good rule of thumb is to remove duplicated knowledge, not merely duplicated characters.
Key takeaways
- DRY means each piece of knowledge or logic should live in exactly one place.
- Duplicated logic must be changed in several places, which invites bugs.
- Extract shared code into functions, modules, constants, or components.
- Code that merely looks similar isn't always true duplication.
- The wrong abstraction can be worse than a little duplication.
Example
// Not DRY: the same tax rule is copied in two places
function cartTotal(cart) {
return cart.subtotal * 1.2;
}
function invoiceTotal(invoice) {
return invoice.amount * 1.2; // if the rate changes, both must be updated
}
// DRY: the rule lives in one place and is reused
const TAX_RATE = 0.2;
const withTax = (amount) => amount * (1 + TAX_RATE);
const cartTotalDry = (cart) => withTax(cart.subtotal);
const invoiceTotalDry = (invoice) => withTax(invoice.amount);Readers ask
What does DRY stand for in programming?
DRY stands for Don't Repeat Yourself. It is the principle that every piece of knowledge or logic in a system should be defined in one place rather than duplicated.
Can you overuse the DRY principle?
Yes. Forcing code that only looks similar into one shared abstraction can couple unrelated features, so a change for one breaks the other. A little duplication is often cheaper than the wrong abstraction.
What is the opposite of DRY?
The playful opposite is WET, often expanded as write everything twice, which describes code with needless duplication. Some developers follow the rule of three: tolerate a second copy, and extract shared code once the same logic appears a third time.
See also
- RefactoringSoftware Architecture, p. 33Refactoring is the process of restructuring existing code to make it cleaner and easier to maintain without changing what the code does from the outside.
- FunctionProgramming Fundamentals, p. 21A function is a named, reusable block of code that performs a specific task, optionally taking inputs called parameters and returning a result.
- Separation of ConcernsSoftware Architecture, p. 37Separation of concerns is a design principle that divides a program into distinct parts, each responsible for one clearly defined aspect of its behavior.
- Technical DebtSoftware Architecture, p. 45Technical debt is the future cost of extra work created when developers choose a quick or limited solution now instead of a better approach that takes longer.
- SOLIDSoftware Architecture, p. 42SOLID is a set of five object-oriented design principles that help developers write code that is easier to understand, extend, test, and maintain.
- Design PatternSoftware Architecture, p. 13A design pattern is a proven, reusable solution to a common problem in software design, described as a general template rather than as finished code.
Spotted a mistake or something missing on this page?Suggest an edit