Lesson 5 · Build well · 8 min read
One source of truth
When the same fact lives in two places, those places will drift apart. Keep each fact in one place, and turn "keep in sync" notes into checks.
Here's a bug that happens in many companies. The product page shows a price. The checkout uses a different table to charge the customer. A script keeps them in sync, and someone has to remember to run it. One day the price drops, nobody runs the script, and customers pay more than they saw.
Nobody wrote "bad" code. The problem was the design: the same fact lived in two places. This lesson is about avoiding that.
What "one source of truth" means
Every important fact (a price, a color, a limit, a rule) should live in exactly one place. Everything else reads from that place.
When you need to change the fact, you change it once. Nothing can drift, because there's nothing to drift from.
No magic values
A "magic value" is a raw number or string in the code with no name and no single home.
Magic values
if (cart.total > 75) showFreeShipping();
// ...in another file
const banner = 'Free shipping over $75!';
One named value
// config.ts
export const FREE_SHIPPING_MIN = 75;
if (cart.total > FREE_SHIPPING_MIN) showFreeShipping();
const banner = `Free shipping over ${FREE_SHIPPING_MIN}!`;
In the first version, someone changes the rule to 50 and forgets the banner. Now the site promises one thing and does another. In the second version, that can't happen.
The same idea applies to design. If your project has design tokens (named colors, spacing and font sizes), use them. Don't paste #1a73e8 into a component. When the brand color changes, you want to change it once, not search through 40 files.
What users see should be what the system does
The price example above is the most dangerous kind of drift: the screen says one thing and the system enforces another. Watch for it with:
- Prices and totals shown vs. charged.
- Limits shown in the form vs. checked on the server.
- Rules in the help text vs. in the code.
The fix is to make both read the same record. The page and the checkout read the same price table. The form and the server read the same limit. If that's truly impossible, write both in a single step that either fully succeeds or fully fails.
A comment is not a guard
You've probably seen comments like this:
// IMPORTANT: keep this list in sync with the server's ALLOWED_TYPES
const allowedTypes = ['jpg', 'png', 'webp'];
This comment will fail. Not today, but eventually. Someone will update one list and miss the comment. People don't read every comment, and the computer doesn't read comments at all.
Turn the comment into a test that fails when the two drift:
import { ALLOWED_TYPES } from '../server/uploads';
import { allowedTypes } from '../client/uploads';
test('client and server allow the same file types', () => {
expect([...allowedTypes].sort()).toEqual([...ALLOWED_TYPES].sort());
});
Now the rule is checked every time CI runs. Even better, import the list from one place so there's only one list.
Derive, don't copy
If you can calculate a value from other data, calculate it. Don't store a copy that can go stale.
- Don't store
itemCountnext to theitemsarray. Useitems.length. - Don't store "is up to date" as a flag. Compare the actual content.
- Don't save a total that you can add up from the lines.
Here's a sneaky example. A tool checked if some data was "fresh" by comparing timestamps. But when a write changed nothing, the timestamp didn't update. So the tool kept saying the data was stale, even though it was correct. The fix was to compare the actual content, not a copy of "when it changed."
Caching is sometimes needed for speed. When you do cache, know exactly what makes the cache wrong, and clear it then.
Duplication is cheaper than the wrong abstraction
Now, a warning in the other direction. "One source of truth" is about facts. It's not a rule that every two similar-looking pieces of code must be merged.
Sandi Metz put it well: duplication is far cheaper than the wrong abstraction. If you merge two things too early, you end up with one function full of if statements and flags, trying to be two things at once.
A good guide is the rule of three:
- The first time, just write it.
- The second time, notice the copy, but it's okay to duplicate.
- The third time, you now understand the pattern. Pull it into one shared piece.
A small sign that you merged too early: a component with a boolean prop that switches between two different behaviors. That's usually two components pretending to be one.
Write each fact once in your docs too
This idea isn't only for code. The same is true for documentation, setup guides, and even instructions you give to AI tools.
If the setup steps live in the README and in a wiki page, one of them is already out of date. If two docs disagree, people (and AI agents) will follow whichever one they read last.
So write each fact once, and link to it from other places. When you change how something works, search the docs for the old behavior and update it.
# You renamed the env var. Find every doc that still says the old name.
git grep -n "API_URL" -- '*.md'
Practice · 20 min
Magic value hunt
Open a project you work on. Search for raw numbers and hex colors (for example git grep -nE "#[0-9a-fA-F]{6}"). Pick one value that appears in more than one place. Give it a name in one place and make the others read from it.
Practice · 30 min
Turn a comment into a guard
Find a comment that says "keep in sync," "must match" or "don't forget to update." Write a test that fails when the two things drift apart. If you can't find one, look in an open-source project you like.
Practice · 15 min
One fact, one place
Pick one fact about a project, like how to run it locally. Search for every place it's written. If there's more than one, keep the best one and replace the others with a link.
Remember
- Every important fact lives in one place. Everything else reads from there.
- Name your values. Use design tokens and config instead of raw numbers and colors.
- What users see must come from the same source as what the system enforces.
- "Keep in sync" comments fail. Replace them with tests.
- Calculate values instead of storing copies that can go stale.
- Don't merge code too early. Wait for the third copy.