Intuition The one core idea
Every fact, rule, or decision your program relies on should live in exactly one place , and everything else should point to that place. When you need to change it, you change it once — never hunting through copies hoping you found them all.
Before you can use DRY, you need to understand the small pieces the DRY — Don't Repeat Yourself note quietly assumes you already know: what "knowledge" means in code, what a variable, function, and constant are , and the two tiny bits of maths (n ⋅ c and ( 1 − p ) n ) it uses to prove duplication is dangerous. We build each from zero.
The parent note keeps saying DRY is about knowledge , not text . This is the single idea everything else hangs from, so we anchor it first.
Definition Knowledge (in the DRY sense)
A single decision or fact your program depends on — for example "sales tax is 18%", "these are the 50 US states", or "a total is price plus tax". It is the meaning , not the letters used to write it.
The picture: imagine each decision as a glowing dot . Wherever your code uses that decision, it should be a wire running back to the one dot — not a copy of the dot.
Intuition Why the topic needs this word
Without separating "knowledge" from "text", you can't tell a real DRY violation (two wires that should be one dot) from a fake one (two dots that happen to look alike). Everything in the parent — the mistakes section, the Rule of Three, the whole point — collapses without this distinction.
A name attached to a stored value so you can refer to the value by its name instead of writing the value out each time. In price_a = 100, the name price_a is the variable and 100 is the value inside it.
The picture: a labelled box . The label is price_a; the thing inside is 100. Ask for the label, get whatever is inside.
Intuition Why the topic needs it
DRY's examples (total_a, price_b) are all variables. You can't discuss "the value lives here" until you know a name can hold a value in one spot.
A variable that is meant to hold one fixed value for the whole program , usually written in CAPITALS (e.g. TAX_RATE = 0.18) as a signal: "this is the authoritative value — read it, don't retype it."
The picture: the same labelled box as a variable, but with a lock on it. The lock isn't magic; it's a convention that says "this is the one true home of this fact."
Intuition Why the topic needs it
Worked Example 1 puts the 18% tax into TAX_RATE. That constant is the "single home" DRY talks about for repeated values . Without the idea of a constant, "the fact lives once" has no place to live.
A named block of steps that takes some input, does work, and gives back a result. You define the steps once under a name, then call that name wherever you need those steps.
The picture below: a machine. Inputs go in the left funnel, the steps run inside, the result drops out the right. with_tax(price) is the machine; feeding it price_a or price_b reuses the same inside.
Definition Call vs define
Define = build the machine once (def with_tax(price): ...).
Call = press its button with some input (with_tax(price_a)).
Defining is where the knowledge lives; calling is the wire pointing back to it.
Intuition Why the topic needs it
A function is the "single named home" for repeated logic . Worked Example 1 turns two copies of "price + price×tax" into one function called twice. This is the mechanism of DRY, and it connects to Refactoring — Extract Method .
A construction that says do this same block of steps once for each item in a list. for label in ["Save", "Cancel", "Help"]: runs draw_button(label) three times, once per label.
The picture: one arrow bending back on itself, with a little tray of items feeding through it one at a time.
Intuition Why the topic needs it
Worked Example 3 replaces three near-identical draw_button(...) lines with one loop. The procedure is written once; only the data varies. This is DRY for repeated structure .
Definition Single Source of Truth
The idea that a piece of data (e.g. "the list of US states") lives in exactly one place , and every other part of the system reads from that place rather than keeping its own copy.
The picture: go back to the glowing-dot figure — this is the same idea applied to data instead of logic. One dot (the states list), many wires (frontend dropdown, backend validator) pointing at it. See Single Source of Truth .
Intuition Why the topic needs it
Worked Example 2 (the US-states list) is literally this. It's DRY's answer for duplicated facts across layers of a program.
The parent proves duplication is bad with a "cost-of-change model". It uses four symbols. We define each from zero.
n = the number of copies of the same knowledge (how many places you'd have to edit). n = 1 means DRY.
c = the cost to edit one copy correctly (time and risk for a single spot).
p = the probability you slip and forget/mess up one copy. A number between 0 and 1 (e.g. p = 0.1 means a 10% chance per copy).
The dot ⋅ just means multiply : n ⋅ c is "n times c ".
If editing one copy costs c , and there are n copies, the total is n lots of c :
C edit = n ⋅ c
The picture: n identical bricks stacked. Two copies = two bricks of work; five copies = five bricks. Cost grows in a straight line with n (this is what "linear" means).
Intuition Why multiplication and not, say, addition
Because each copy costs the same fixed amount c and they're independent chunks of work — repeated equal additions (c + c + … , n times) is multiplication n ⋅ c . That's the exact tool for "the same effort, repeated a countable number of times."
Now the risk. For all n copies to stay correct, you must avoid slipping on copy 1 and copy 2 and … copy n .
Chance you don't slip on one copy = 1 − p .
"This AND that" for independent chances means multiply the chances.
Multiplying ( 1 − p ) by itself n times is written as a power :
P consistent = ( 1 − p ) n
Here ( 1 − p ) n is shorthand for n times ( 1 − p ) × ( 1 − p ) × ⋯ .
Intuition Why a power and not multiplication by
n
Multiplying by n would answer "how many copies?". But we're chaining independent successes — each extra copy is another gamble you must also win. Winning gamble-after-gamble multiplies probabilities, and multiplying the same factor repeatedly is exactly what a power is. That's why the reliability decays (each factor 1 − p is below 1, so the product shrinks) instead of growing.
The picture: two curves against n . Cost climbs as a straight line; consistency plunges as a curve toward zero.
( 1 − p ) n as ( 1 − p ) ⋅ n
Feels right: both have n in them.
Wrong because: the superscript n means repeated multiplication (a power), not "times n ". ( 0.9 ) 5 ≈ 0.59 , but 0.9 ⋅ 5 = 4.5 — a nonsense probability above 1. The power is what makes reliability collapse .
Function = reusable machine
Power one minus p to the n
In the DRY sense, "knowledge" means? A single decision or fact the program depends on — its meaning, not the letters used to write it.
A variable is? A name attached to a stored value, so you refer to the value by its name.
A constant is (and why CAPITALS)? A variable meant to hold one fixed value for the whole program; CAPITALS signal "this is the authoritative home of this value."
Define vs call a function? Define builds the machine once; call presses its button with an input. The knowledge lives at the definition; each call is a wire back to it.
What does a loop let you write once? The repeated procedure/structure — you describe the action once and run it per item in a list.
Single Source of Truth means? A piece of data lives in exactly one place and everything else reads from it.
What is n in the cost model? The number of copies of the same knowledge (n=1 is DRY).
What is p ? The probability you slip on one copy, a number between 0 and 1.
Why is edit cost n ⋅ c (multiply)? Each of the n copies costs the same fixed c, and repeated equal additions of c is multiplication.
Why is consistency ( 1 − p ) n (a power)? You must avoid slipping on every copy independently; chaining independent successes multiplies (1-p) by itself n times.
What does ( 0.9 ) 5 equal, roughly? About 0.59 (≈ 59%), not 4.5 — the superscript is a power, not "times 5".