Design principles DRY, KISS and YAGNI

DRY, KISS and YAGNI

Three simplicity rules, and what to do when they point in different directions.

  • 11 min read
  • 6 chapters
  • Lesson 3 of 4

What you will learn

  1. 01Three short rules
  2. 02DRY: do not repeat knowledge
  3. 03KISS: keep it simple
  4. 04YAGNI: you are not going to need it
  5. 05When they conflict
  6. 06A decision checklist
01

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.

DRYREQone source of truthKISSCLOCKsimplest that worksYAGNICHECKbuild when neededYour codeHLDsmall and calm

Swipe sideways to pan · drag a box to move it

100%
Three rules about the cost of code.
02

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.

DRYREQone source of truthKISSCLOCKsimplest that worksYAGNICHECKbuild when neededYour codeHLDsmall and calm

Swipe sideways to pan · drag a box to move it

100%
Write each piece of knowledge once.
03

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.

DRYREQone source of truthKISSCLOCKsimplest that worksYAGNICHECKbuild when neededYour codeHLDsmall and calm

Swipe sideways to pan · drag a box to move it

100%
Choose the simplest design that works today.
04

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.

05

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.

DRYREQone source of truthKISSCLOCKsimplest that worksYAGNICHECKbuild when neededYour codeHLDsmall and calm

Swipe sideways to pan · drag a box to move it

100%
Do not build for a future you only imagine.
06

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.