System Design Academy

Saving…

Lesson 7 of 7 in Module 20 of 45 lessons complete

DRY, KISS & YAGNI

Module 2, lesson 7Concept12 min

Definition

DRY

In plain English
DRY, short for Don't Repeat Yourself, means each piece of knowledge should live in exactly one place in the code.
Think of it like
It is like keeping one master copy of a recipe instead of five slightly different photocopies.

KISS

In plain English
KISS, short for Keep It Simple, means choosing the simplest design that solves the problem.
Think of it like
It is like using a door wedge instead of building a motorised door stopper.

YAGNI

In plain English
YAGNI, short for You Aren't Gonna Need It, means not building features or flexibility until a real need appears.
Think of it like
It is like not buying a seven-seat car for children you might have someday.

Why it exists

You need one CSV export. A general system for plugging in exporters adds setup code before anyone needs a second format.

You also find four copies of one tax rate. One changes, and receipt totals disagree. You need fewer repeated rules, not more machinery.

How it works

Accidental duplication

In plain English
Accidental duplication is code that looks the same today but represents different rules that will change separately.
Think of it like
Like two roads that run side by side for a mile and then split.

Plugin

In plain English
A plugin is a separately supplied part that adds behaviour through an agreed connection.
Think of it like
It is like an extra tool head that fits the same handle.
PrincipleWhat it preventsMisuse or neglect
DRYRepeated knowledge drifting apartOveruse tangles unrelated rules in one function
KISSDesigns that are hard to followIgnoring it produces clever but unreadable code
YAGNIUnused features and flexibilityIgnoring it produces a plugin system with one plugin

A decision asks whether two copies express the same rule. Yes leads to one shared function; no leads to separate rules.

Share knowledge, not accidental resemblance.

A decision asks whether two copies express the same rule. Yes leads to one shared function; no leads to separate rules.

Do not delay a known security requirement or a necessary stored-data format decision. YAGNI targets imagined needs, not confirmed requirements.

Examples

Keep one tax rule

Four receipt paths repeat an 8% tax rate. Updating only one makes the receipts disagree.

Before: calculate the same tax in four paths using integer cents.
Go
package main

import "fmt"

func main() {
	shelf := 10000 * 8 / 100
	receipt := 10000 * 8 / 100
	refund := 10000 * 8 / 100
	report := 10000 * 8 / 100
	fmt.Println(shelf, receipt, refund, report)
}
After: calculate the same tax in four paths using integer cents.
Go
package main

import "fmt"

const taxPercent = 8

func Tax(cents int) int { return cents * taxPercent / 100 }
func main() {
	shelf := Tax(10000)
	receipt := Tax(10000)
	refund := Tax(10000)
	report := Tax(10000)
	fmt.Println(shelf, receipt, refund, report)
}

The insight

Duplicated knowledge can drift apart.

Trade-offs

Pros

  • Less code to understand.
  • Fewer places to edit one rule.
  • Less behaviour to test.

Cons

  • Blind DRY couples unrelated rules.
  • Misusing YAGNI can delay a certain requirement.

Use when

  • Use these as everyday defaults.
  • Check whether new flexibility has a real user.

Avoid when

  • A confirmed security need must be handled now.
  • A stored-data format decision will be expensive to change later.

Quick check

Day-pass and staff-pass checks are identical today, but staff rules are expected to change. Should you merge them into one function?
A team builds a plugin system for report exports, but only CSV export was requested. Which principle does this break?

Takeaways

  • DRY: one place per piece of knowledge, but don't merge things that only look alike.
  • KISS: the simplest design that works.
  • YAGNI: build flexibility when the need is real.

Try the quick check above whenever you feel ready.

Previous: D — Dependency Inversion & Dependency Injection