SOLID & DESIGN PRINCIPLES / OBJECT DESIGN BRIEF

DRY, KISS, and YAGNI

DRY (Don't Repeat Yourself): every piece of knowledge should have a single authoritative representation.

BeginnerPhase 02 / Topic 6 of 8ResponsibilitiesCollaborationsExtensibility
01

Overview

DRY (Don't Repeat Yourself): every piece of knowledge should have a single authoritative representation. If the tax rule lives in three places, a change will eventually miss one. KISS (Keep It Simple, Stupid): prefer the simplest design that works. YAGNI (You Aren't Gonna Need It): do not build features or abstractions until they are actually needed.

These principles balance each other against over- and under-engineering. DRY is about duplicated knowledge, not identical-looking code; two similar lines that change for different reasons should stay separate. KISS and YAGNI prevent speculative abstractions, while DRY and SOLID prevent tangled duplication.

Packing for a trip

DRY: keep one master packing list instead of three that drift apart. KISS: pack a simple outfit system rather than a complicated one. YAGNI: do not pack a snowsuit for a beach trip 'just in case'.

02

When to use it

  • Reviewing designs for over-engineering or copy-paste logic.
  • Deciding whether to introduce an abstraction now or later.
  • Interview discussions about trade-offs and pragmatism.
03

Where it shows up in interviews

Over-engineering check

Recognize it when: interviewer asks 'is this needed?'

  • Design a simple URL shortener class
  • Design a to-do app
Duplicated rules

Recognize it when: same business rule in several classes.

  • Refactor pricing logic
  • Refactor validation rules
04

Where it is used in real software

The Pragmatic Programmer

Introduced DRY as 'every piece of knowledge must have a single, unambiguous representation'.

Extreme Programming

YAGNI came from XP practice: implement only what current stories need.

Rule of three

Many teams refactor to a shared abstraction only when duplication appears the third time.

05

Key terms

Knowledge duplication
The same rule or fact encoded in multiple places.
Incidental duplication
Similar code that represents different concepts.
Speculative generality
Abstractions for hypothetical future needs.
Rule of three
Abstract after the third repetition.
06

How it works, step by step

  1. 1
    Spot duplicated knowledge

    Same rule, constant, or validation in several places.

  2. 2
    Check it changes for the same reason

    If yes, unify; if not, keep separate.

  3. 3
    Extract the single source

    A function, constant, value object, or config.

  4. 4
    Question every abstraction

    Is there a current requirement or second implementation?

  5. 5
    Prefer the simplest thing that works

    Refactor when requirements actually change.

07

Applying the principles

Examples from a shop codebase

Step 1 / 4
SituationPrincipleAction
Tax rate 0.18 hard-coded in 4 filesDRYOne TaxPolicy or config value
Plugin system for one payment providerYAGNIDirect implementation behind an interface
Generic rule engine for 2 discount typesKISSTwo small classes
Similar validation for User and Product namesDRY? NoKeep separate: rules change independently

NOWSituation: Tax rate 0.18 hard-coded in 4 files | Principle: DRY | Action: One TaxPolicy or config value

Duplication of knowledge is costly; duplication of shape is often fine. Build for today's requirements with clean seams for tomorrow.

08

Implementation

// DRY: one source of truth for a business ruleexport const FREE_SHIPPING_THRESHOLD_CENTS = 5_000;export const qualifiesForFreeShipping = (subtotalCents: number) => subtotalCents >= FREE_SHIPPING_THRESHOLD_CENTS; // Used by cart UI, checkout, and email templates alikefunction shippingCost(subtotalCents: number) {  return qualifiesForFreeShipping(subtotalCents) ? 0 : 599;} // KISS / YAGNI: a plain function instead of a speculative framework// Avoid: class ShippingStrategyFactoryProvider { ... } when one rule exists today.
09

Complexity and performance

Change cost with duplicationO(copies)

And risk of missing one.

Cost of speculative abstractionOngoing complexity

Paid even if never used.

10

Trade-offs

DRY vs coupling

Merging code used by unrelated features couples them; the wrong abstraction is worse than duplication.

YAGNI vs known requirements

YAGNI does not mean ignoring requirements that are confirmed and imminent; leave clean extension points.

11

Variants and related techniques

WET (Write Everything Twice)

Tolerate duplication until the pattern is clear.

AHA (Avoid Hasty Abstractions)

Prefer duplication over the wrong abstraction.

12

Common mistakes

  • Merging similar-looking code with different reasons to change.

    Fix: Keep it separate until it truly represents the same knowledge.

  • Building generic frameworks in interviews.

    Fix: Solve stated requirements cleanly; mention how you would extend.

13

Interview questions

Is all duplication bad?

No. Duplicated knowledge (the same business rule) is bad. Code that looks similar but represents different concepts that change independently should often stay separate.

How do YAGNI and OCP coexist?

Apply OCP where variation is known or recurring; YAGNI warns against creating extension points for speculative variation. Refactor toward OCP when the second variant appears.

14

Practice problems

ProblemDifficultyWhat it trains
Find duplicated rules in a checkout codebaseEasySingle source of truth.
Simplify an over-engineered designMediumKISS and YAGNI.