Skip to the workspace
Pebellslearn by building
Design Principles  ›  Lesson

DRY and YAGNI

DRY 14 minEvery piece of knowledge should live in one place, and code should not be built for needs that have not arrived yet.

software design architecture / DRY and YAGNI

Explore each scene at your own pace.

01 · The rule

The rule

DRY and YAGNI

Shelfwise raised its member discount from 10% to 15%. The cart showed 15%, the invoice still said 10%, and the mobile app used 12% from some older experiment. The same business rule had been written in three places. Meanwhile, a teammate spent a week on a plugin system for "future payment methods", and Shelfwise still only takes cards. Two principles cover these two opposite mistakes.

The rule

Each fact lives in one place; build nothing before it is needed.

DRY — Don't Repeat Yourself. Every piece of knowledge in the system should have one clear home. DRY is about knowledge, not about text that looks alike.

MEMBER_DISCOUNT = 0.10

def member_price(total):
    return total * (1 - MEMBER_DISCOUNT)

The cart, the invoice and the API all call member_price. The next change is one edit.

Two functions can look similar and still not be duplication. Shelfwise's postcode validator and phone validator both loop over characters, but they encode different rules owned by different people, and they will change for different reasons. Merging them would couple unrelated things: a fix to one would risk the other. A popular rule of thumb is to wait for the third copy before extracting, so you can see what is truly shared.

YAGNI — You Aren't Gonna Need It. Do not build features, options or extension points for needs that have not arrived. Speculative code costs time to write, test, review and maintain, and when the real need arrives it often looks different from the guess.

YAGNI does not mean "write rigid code". Clean, small, well-named code is easy to extend later. YAGNI says: keep it easy to change, but do not make the change yet.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.