Skip to main content

DRY

Don't Repeat Yourself

Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/dry-principle

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

Removing duplicated logicjavascript
// 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

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings