Vol. I a living index of named laws · est. mmxxvi no ads · no account
The Law Tomeindex of named laws

The DRY Principle

also known as — Don't Repeat Yourself, DRY, Single Source of Truth, Duplication is evil
"Every piece of knowledge must have a single, unambiguous, authoritative representation within a system."
In plain English

What does The DRY Principle mean?

If the same fact is written down in two places, the two will eventually disagree, because somebody will change one and not the other. The principle says each fact should live in exactly one place, and everything that needs it should refer to that place rather than restate it.

Reliability

Rated Heuristic: a dependable rule of thumb, not a proven theorem. The other 224.

Lineage
1999COINED
How it works

How does The DRY Principle work?

Duplication is not a cost at the moment of writing; it is a cost at the moment of change. Two copies of a fact are consistent only while nobody edits them, and the probability that an edit reaches every copy falls as the copies spread and drift apart in the codebase. The failure is silent: nothing breaks when the copies diverge, so the divergence is discovered later as a bug whose cause is that the system believed two contradictory things at once. Hunt and Thomas frame the unit as knowledge rather than text, which is why the principle is about facts and not about identical-looking lines.

Where you'll see it

What are examples of The DRY Principle?

The field that appears five times Hunt and Thomas's own example: a concept called 'comment' repeated as a form label, an HTML attribute, a function name, a variable and a database column. Renaming it means finding all five, and missing one leaves a system that half-knows the new name.
Validation on both sides A rule about what counts as a valid input is written once in the browser and once on the server. They agree until one is relaxed, after which the system accepts something it is documented to reject.
Documentation as a second copy A README states the default timeout. The default changes in code, the README does not, and the document is now actively misleading, which is why generated documentation is a DRY technique, not a stylistic preference.
Why it matters

Why does The DRY Principle matter?

It names the reason maintenance costs grow faster than codebases do, and it gives the most common review comment in software a principle behind it. It also explains why so many tools exist (constants, code generation, schema-driven builds, single-source configuration) that look like indirection and are really attempts to keep one fact in one place.

Working with it

How do you apply The DRY Principle?

  • Ask whether it is one fact or two — identical code that happens to coincide is not duplication of knowledge. Two rules that must always change together are; two that merely look alike today are not.
  • Prefer a shared source over a shared copy — generate the second representation from the first (types from a schema, docs from code, clients from an interface definition), rather than keeping them in step by hand.
  • Wait for the third occurrence — the rule of three: two instances are a coincidence, three are a pattern with enough evidence to say what the abstraction should be.
Where it breaks down

What are the limits of The DRY Principle?

Applied to text rather than to knowledge it does real harm, because it collapses things that merely resemble each other into one abstraction that then has to serve two masters. Sandi Metz's formulation of the counterweight is blunt: prefer duplication over the wrong abstraction, since duplication is cheap to fix and a wrong abstraction is not. The AHA principle, avoid hasty abstractions, makes the same point as advice about timing. Coupling is the price: every de-duplication creates a dependency, and a dependency across a boundary that should stay independent (two services, two teams, two release cycles) can cost more than the duplicate did.

What it doesn't say

What are common misconceptions about The DRY Principle?

It is not a ban on repeated characters, and 'DRY' is not satisfied by extracting every three-line similarity into a helper. It is also not a claim that all coupling is good: the principle says one fact should have one home, not that two unrelated systems should share code because their code looks alike.

Is it real?

Is The DRY Principle real?

Real as a rule of thumb, not as a theorem. The DRY Principle is rated Heuristic here: dependable enough to plan with, and with no proof behind it.

Rated from two sources. 225 of the 1,116 entries carry this rating. How the scale works.

25 of 69entries in software & systems carry the same rating

The best-known laws are the least likely to rest on measurement.

Origin

Where did The DRY Principle come from?

Formulated by Andy Hunt and Dave Thomas in The Pragmatic Programmer (Addison-Wesley, 1999), where it appears with the wording given here. The opposing backronym WET, 'write everything twice', is later and informal.

Known elsewhere as

What is The DRY Principle called in other languages?

  • ArabicDon't Repeat Yourself
  • GermanDon’t repeat yourself
  • SpanishNo te repitas
  • FrenchNe vous répétez pas
  • Italiandon't repeat yourself
  • JapaneseDon't repeat yourself
  • PortugueseDon't repeat yourself
  • RussianDon’t repeat yourself
  • Chinese一次且仅一次

9 languages, as recorded on Wikidata. These are names the idea already goes by, not translations we made. The whole index by language.

Sources

Sources & further reading

  1. 1Hunt, A. & Thomas, D. (1999). The Pragmatic Programmer: From Journeyman to Master. Addison-Wesley. — the principle, in the wording quoted, and the repeated-'comment' example.primary
  2. 2"Don't repeat yourself." Wikipedia — the canonical wording, the WET backronym, and the AHA / 'prefer duplication over the wrong abstraction' counterweights.secondary

Everything above traces to pragprog.com and en.wikipedia.org, two sources in all, one of them the original publication rather than an account of it. Nothing is written from memory, and the method is written down. Spot an error or a better source? Suggest a fix.

What we checked

  • 1,116entries in the tomeListed one by one on the browse page.
  • 1,116of them citing a sourceOpen any entry: its sources sit at the foot of the page.
  • 2,489source references in allEach is named, and linked wherever the source is online.