الانتقال إلى المحتوى
Software Architectureمبادئ التصميم· 6 دقائق للقراءة

DRY Is About Knowledge, Not Similar-Looking Code

Hunt and Thomas defined DRY as one authoritative home for each piece of knowledge. Look-alike code can be a coincidence, and real duplication often looks nothing alike.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
محتويات المقال

Marketing wants coupon codes like SUMMER-2026. A month ago a reviewer merged isValidUsername and isValidCoupon into one isValidCode, because the two bodies were identical. The developer widens its pattern, coupons work, and signup quietly starts accepting usernames with hyphens and up to 30 characters. The same week, free shipping moves from 50 to 75. The cart is updated, but the banner computes "amount left" with its own 50, so a customer with 60 in the cart is told shipping is free and then charged for it. Two bugs, opposite causes: one merge too many and one too few.

The common misconception

The belief: DRY means any two pieces of code that look alike must be merged into one shared function, base class or utility. If a duplicate-code check flags it, extract it.

It's an easy belief to pick up. Copy-paste detectors in CI report similar lines, not similar intent. Code review rewards spotting repetition. And the authors themselves admit the first edition of The Pragmatic Programmer "did a poor job" of explaining DRY: many readers took it to mean "don't copy-and-paste lines of source." In a 2003 interview Dave Thomas called DRY "probably one of the most misunderstood parts of the book."

The half-truth: copy-pasted code often is duplicated knowledge, and removing it is valuable. But Hunt and Thomas call it "a tiny and fairly trivial part" of DRY.

What actually happens

DRY is about knowledge

The , as the 20th anniversary edition states it: "Every piece of knowledge must have a single, unambiguous, authoritative representation within a system." In the interview Thomas lists what knowledge includes: code, but also database schemas, test plans, the build system and documentation. The reason is practical: when the same thing is expressed in several places, the copies fall out of step. Or, in the book's words, "It isn't a question of whether you'll remember: it's a question of when you'll forget."

The acid test is change, not appearance

The book gives a test: when a single facet of the code has to change, do you find yourself making that change "in multiple places, and in multiple different formats?" If so, the code isn't DRY. Notice what the test looks at: what happens when a rule changes, not whether two lines look alike.

Same code, different knowledge

The book's own example is a wine shop that validates a customer's age and an order quantity. Both must be numbers greater than zero, so the two functions are identical, and a reviewer calls it a DRY violation. The authors disagree: "The code is the same, but the knowledge they represent is different." The two rules just happen to match today: "That's a coincidence, not a duplication." Merging them turns a into : the next change to one rule silently changes the other. Microsoft's architecture guide makes the same point about two constants that share a value but mean different things, and concludes: "Duplication is always preferable to coupling to the wrong abstraction."

Different code, same knowledge

The more dangerous duplication rarely looks like duplication. The book describes DRY as being about expressing the same thing "possibly in two totally different ways": a rule in code and again in a comment, in a schema and in the class that mirrors it. In the hook, subtotal >= 75 and 50 - subtotal share no text at all, so no detector will ever flag them, yet they encode one business fact.

Removing real duplication still pays

None of this makes DRY weaker. Martin Fowler wrote in 2001 that the principle is simply to "say anything in your program only once", and that a determined effort to remove repetition leads a long way toward good design. He also notes that coincidental duplication exists, but finds it "rare and easy to spot". Kent Beck's rules of simple design include "no duplication", which Beck phrased as saying things "Once and only Once". The skill is aiming the principle at knowledge.

The fix: one home per fact, not per shape

TypeScript
// validation.ts: merged because both bodies looked identicalexport const isValidCode = (v: string) => /^[A-Za-z0-9-]{3,30}$/.test(v);// Widened for coupons, so usernames now accept hyphens too// cart.ts: the free-shipping rule, updated to 75...export const freeShipping = (subtotal: number) => subtotal >= 75;// banner.ts: ...and the same rule again, in a shape no detector flagsexport const amountToFreeShipping = (subtotal: number) =>  Math.max(0, 50 - subtotal);

The two validators are separate again, even though they still look similar, because the account team and marketing will change them for different reasons. The shipping threshold, which looked like two unrelated expressions, now lives in one constant that both functions read. If finance moves it to 100, that's a one-line change, and the banner can't disagree with the cart.

MythReality
DRY means no similar-looking codeDRY means one authoritative representation per piece of knowledge
Identical code is always duplicationIdentical code can encode different rules; Hunt and Thomas call that a coincidence
Code that looks different can't be duplicatedOne business rule can hide in code, comments, schemas and SQL that share no text
A duplicate-code tool finds DRY violationsIt finds repeated text; only the change test finds repeated knowledge
DRY is only about codeThomas: knowledge includes schemas, test plans, the build and documentation

How to apply it in your project

  1. Triage duplicate-code warnings with the change testFor each flagged pair, decide whether one rule changing must always change the other. Merge true duplicates; mark coincidences so the next reviewer doesn't merge them.
  2. Hunt for duplicates that don't look alikeSearch for business numbers and names across layers: thresholds, rates, limits, status names. Check the frontend, the backend, SQL, configuration and docs.
  3. Give each fact one authoritative homePut it in one constant, table or config value, and derive the rest from it. For schemas and APIs, Hunt and Thomas suggest generating code from one source, such as a database schema or an OpenAPI spec.
  4. Undo wrong mergesWhere a shared helper has collected flags, copy it back into each caller, delete the flags, and let each version follow its own rule.

Key takeaways

  • DRY means every piece of knowledge has one authoritative representation, not "no similar-looking code".
  • Identical code can encode different rules; merging it couples changes that should stay separate.
  • The worst duplication often looks nothing alike: the same rule in different code, schemas or docs.
  • Judge duplication by change: if one copy changing must always change the other, it's one piece of knowledge.
  • A shared helper that keeps growing flags is a sign the abstraction was wrong; un-merging it is a valid refactoring.

📚 المصادر

  1. The Evils of Duplication — Dave Thomas and Andy Hunt, The Pragmatic Programmer, 20th Anniversary Edition (excerpt)
  2. Orthogonality and the DRY Principle — A Conversation with Andy Hunt and Dave Thomas, Artima (2003)
  3. Avoiding Repetition — Martin Fowler, IEEE Software (2001)
  4. Beck Design Rules — Martin Fowler
  5. Architectural principles — .NET Architecture Guides, Microsoft Learn

التعليقات (0)