DRY, KISS and YAGNI
Three simplicity rules, and what to do when they point in different directions.
What you will learn
- 01Three short rules
- 02DRY: do not repeat knowledge
- 03KISS: keep it simple
- 04YAGNI: you are not going to need it
- 05When they conflict
- 06A decision checklist
Three short rules
These three are the most quoted lines in design, and the most misused. Each is about cost. Used with judgment they keep code small and calm. Used blindly they create clutter.
Swipe sideways to pan · drag a box to move it
DRY: do not repeat knowledge
Don't Repeat Yourself says every piece of knowledge should have one authoritative place. The key word is knowledge, not text. If the hourly rate is written in three functions, changing it means three edits and a chance to miss one.
const HOURLY_RATE = 20;
const fee = (m: number) => Math.ceil(m / 60) * HOURLY_RATE;
Not all repetition is a violation. Two blocks that look alike but change for different reasons are only coincidentally similar. Merging them couples things that should be free. A useful habit is the rule of three: tolerate a second copy, and abstract on the third, when the real shape is visible.
Swipe sideways to pan · drag a box to move it
KISS: keep it simple
Keep It Simple says choose the simplest design that meets today's requirement. Every extra layer, class and option is something the next reader must understand. A plain function with an if is often better than a clever framework of plugins. Simple does not mean easy to write. It means easy to read, change and delete.
Swipe sideways to pan · drag a box to move it
YAGNI: you are not going to need it
You Aren't Gonna Need It says do not build for a future you are only imagining. Speculative features cost time now, add code to maintain, and are usually wrong when the real need arrives. Build what is required, and keep the design clean enough to change.
YAGNI is not a license for sloppy structure. Leaving seams, such as an interface at a likely change point, costs little. Building the whole extension now does not.
When they conflict
DRY can push you to merge things too early, and the shared abstraction then grows flags and special cases to serve callers that differ. That breaks KISS. A good guide is that duplication is cheaper than the wrong abstraction, so wait until you understand the pattern.
Swipe sideways to pan · drag a box to move it
A decision checklist
- Is this one piece of knowledge written in several places? Consolidate.
- Do the copies change for the same reason? If not, leave them apart.
- Is there a simpler version that passes today's tests? Use it.
- Am I building for a requirement nobody has stated? Stop.