Skip to content
BENIBIX Learn

DRY, KISS and YAGNI

Level 6 · Good code habits

Three short rules of thumb for writing code: do not repeat yourself, keep it simple, and do not build what you do not need yet. Plus SOLID.

Course contentsLesson 18 of 18

After this lesson you can

  • Remove a repeat with a function
  • Choose the simplest design that works
  • Say what the five SOLID rules are for

11 mincovers 4 glossary entries

Worth reading first: Clean Code and Naming

Which rules of thumb keep code easy to change

Three short rules guide most everyday code. DRY means don't repeat yourself: each piece of logic should live in one place. KISS means keep it simple: pick the plainest design that does the job. YAGNI means you aren't gonna need it: do not build a feature until it is actually needed.

DRY comes from a 1999 book about the craft of writing code. The danger it guards against is a fix made in one copy and missed in another. If the same 5 lines of maths sit in 4 places, a change means 4 edits. Put them in one function and call it 4 times.

DRY is about the same knowledge, not the same-looking text. Two lines that happen to match today, but mean different things, can be left apart. Joining them forces them to change together, and one day they will need to differ.

KISS warns against clever code. A plain loop that anyone can follow beats a one-line trick that only its author understands. Simple code has fewer places for bugs to hide, and it is quicker to change. When two designs both work, pick the one a beginner could read.

YAGNI warns against building for a future that may never come. Code written for later still has to be read, tested and kept working now. Most of those later features never arrive, or arrive looking different. Build what this week needs, and keep the code easy to change.

SOLID is a set of five rules for the bigger building blocks of code, called classes. Each letter names one rule. You will meet classes once programs grow. For now, the first rule is the one to keep. It says each part of the code should have one job, and only one.

These are rules of thumb, not laws, and they can pull against each other. Squeezing out every repeat can make code less simple. The goal behind all of them is the same: code that the next person can read and change without fear.

Which rule to reach for

  1. 1

    DRY

    Reach for it when you are about to paste the same logic a second time.

  2. 2

    KISS

    Reach for it when your solution needs a long comment to explain how it works.

  3. 3

    YAGNI

    Reach for it when you catch yourself saying we might need this one day.

  4. 4

    SOLID

    Reach for it later, once your programs are built from classes that work together.

Logic in one place is changed in one place. Copies drift apart the first time someone misses one.

Worked example

Lucy skips the export button

Lucy's team asks for a sign-up form. She is tempted to add export to 4 file types as well, in case someone asks.

She builds only the form, in 2 days. Six months later, nobody has asked for an export. YAGNI saved her about a week of code she would have had to keep working.

The words for this lesson4

Check yourself

What does the YAGNI rule tell you to do?

Finished this lesson?

Marking it complete also marks its 4 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.