DEV Community

Adam Lewis
Adam Lewis

Posted on Originally published at prickles.org AI-assisted

Don't Repeat Yourself: the second sentence is the one that matters

I've watched DRY get applied as a reflex more than as a rule. Someone reads the Pragmatic Programmer, sees two similar lines a week later, and extracts a helper. Six months on the helper is a flag for every caller and a name that no longer covers any of them.

The fix isn't to abandon DRY. It's to read the second sentence Hunt and Thomas wrote and notice the word knowledge. Every piece of knowledge must have a single, unambiguous, authoritative representation within a system. Two copies of a fact mean the next maintainer changes one and the bug goes out from the unchanged second, and git-blame won't help you because the lie was committed twice.

Coupling it to F4

The rule I apply is extract on the second occurrence, never extract across a bounded context. Inside one context, deduplication is cheap insurance. Across two, you're usually sharing a shape rather than a fact.

The reflex is the cost

The reflex extraction is what gives DRY its bad reputation. Two functions that both take an order and a customer aren't knowledge, they're two decisions that happen to look alike today. Merging them makes a third thing that answers to neither caller.

Nothing to do with line counts

A duplicated constant is knowledge and a duplicated loop body usually isn't. Would the two copies have to change together for the system to stay correct? If yes, one home. If no, leave them.

Agents make it worse

An agent that spots two similar functions will merge them without asking whether they share a fact. The merged helper then becomes the vocabulary for everything written afterwards. The duplication was visible. The wrong abstraction is not.

Full write-up: https://prickles.org/tenet/dont-repeat-yourself/F3

Top comments (0)