Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
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.

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.

The shortcut

For a suspected duplicate, ask: if this rule changed, would every copy have to change together? Yes: it is one fact, give it one home. No: leave the copies apart.

For a proposed feature, ask: is anyone asking for this today? If not, write it down and move on.

Three Shelfwise review commentsStep 1 of 4
  1. total * 0.9 in three files

    If the discount changed, all three must change together. One fact, so extract member_price.

The trap is "DRY at any cost": a single over-clever function with five flags that serves ten callers who only looked alike.

Drills

Answer fast — trust the rule.

quick drill

What does DRY say should exist only once?

quick drill

The free-shipping threshold 500 appears in checkout, the banner text and the shipping calculator. What should you do?

quick drill

A teammate wants to add multi-currency support "in case we expand abroad". Nobody has asked for it. What does YAGNI say?

quick drill

Which is a sign that DRY has been taken too far?

Remember

  • Each fact lives in one place; build nothing before it is needed.
  • Ask: would every copy have to change together? Is anyone asking for this today?
  • Trap: merging look-alike code into one flag-heavy function.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainDuplication worth removing?

Two Shelfwise cases: (1) the rule 'members get 10% off' appears as total * 0.9 in the cart, the invoice and the mobile API; (2) the functions that validate a postcode and a phone number happen to have similar-looking loops. A teammate also wants to add a plugin system for 'future payment methods' although Shelfwise only takes cards. Explain what DRY and YAGNI say about each.