2.2.1 · D1Design Principles

Foundations — DRY — Don't Repeat Yourself

1,857 words8 min readBack to topic

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 ( and ) it uses to prove duplication is dangerous. We build each from zero.


0. The word "knowledge" (the most important word on the page)

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.

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.

Figure — DRY — Don't Repeat Yourself

1. Variable — a named box that holds a value

The picture: a labelled box. The label is price_a; the thing inside is 100. Ask for the label, get whatever is inside.


2. Constant — a box you promise never to change

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."


3. Function — a reusable machine you name once and call many times

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.

Figure — DRY — Don't Repeat Yourself

4. Loop — describe a repeated action once, run it over a list

The picture: one arrow bending back on itself, with a little tray of items feeding through it one at a time.


5. Single Source of Truth — one authoritative copy that others read

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.


6. The two tiny bits of maths

The parent proves duplication is bad with a "cost-of-change model". It uses four symbols. We define each from zero.

6a. Why multiplication gives edit cost

If editing one copy costs , and there are copies, the total is lots of :

The picture: identical bricks stacked. Two copies = two bricks of work; five copies = five bricks. Cost grows in a straight line with (this is what "linear" means).

Figure — DRY — Don't Repeat Yourself

6b. Why a power gives the consistency probability

Now the risk. For all copies to stay correct, you must avoid slipping on copy 1 and copy 2 and … copy .

  • Chance you don't slip on one copy .
  • "This AND that" for independent chances means multiply the chances.
  • Multiplying by itself times is written as a power:

Here is shorthand for .

The picture: two curves against . Cost climbs as a straight line; consistency plunges as a curve toward zero.

Figure — DRY — Don't Repeat Yourself

Prerequisite map

Knowledge vs text

DRY principle

Variable = named box

Constant = locked box

Function = reusable machine

Loop over a list

Single Source of Truth

Multiply n times c

Cost-of-change model

Power one minus p to the n


Equipment checklist

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 in the cost model?
The number of copies of the same knowledge (n=1 is DRY).
What is ?
The probability you slip on one copy, a number between 0 and 1.
Why is edit cost (multiply)?
Each of the n copies costs the same fixed c, and repeated equal additions of c is multiplication.
Why is consistency (a power)?
You must avoid slipping on every copy independently; chaining independent successes multiplies (1-p) by itself n times.
What does equal, roughly?
About 0.59 (≈ 59%), not 4.5 — the superscript is a power, not "times 5".