DRY, KISS & YAGNI
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.
| Principle | What it prevents | Misuse or neglect |
|---|---|---|
| DRY | Repeated knowledge drifting apart | Overuse tangles unrelated rules in one function |
| KISS | Designs that are hard to follow | Ignoring it produces clever but unreadable code |
| YAGNI | Unused features and flexibility | Ignoring 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.
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.
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)
}
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.
Use the actual matching rule
An engineer built a JSON rule engine. Matching currently needs only the same region and a skill difference within 100 points.
JSON
- In plain English
- JSON is a text format that stores named values and lists.
- Think of it like
- It is like a labelled form that another program can read.
Rule engine
- In plain English
- A rule engine is code that reads stored rules and decides which actions to take.
- Think of it like
- It is like a referee following instructions written on a separate card.
package main
import "fmt"
type Player struct {
Skill int
Region string
}
func Match(a, b Player) bool {
gap := a.Skill - b.Skill
if gap < 0 {
gap = -gap
}
return a.Region == b.Region && gap <= 100
}
func main() {
fmt.Println(Match(Player{1200, "east"}, Player{1250, "east"}))
}
The function states both rules together. You can test a different region or a gap of 101 without configuring an engine.
The insight
The direct version is easier to read, test and change.
Build only the required passes
One resort sells day passes in euros. Future staff-pass rules will differ, even though both checks currently use the same date.
package main
import "fmt"
type DayPass struct {
Day string
EuroCents int
}
type StaffPass struct{ Day string }
func ValidDayPass(p DayPass, today string) bool { return p.Day == today }
func ValidStaffPass(p StaffPass, today string) bool { return p.Day == today }
func main() {
today := "2030-01-02"
fmt.Println(ValidDayPass(DayPass{today, 4500}, today))
fmt.Println(ValidStaffPass(StaffPass{today}, today))
}
There is no currency selector, resort registry or system for plugging in pass types. Keep the staff check separate because it represents another rule.
The insight
Build for real needs, and keep rules separate when they only look alike.
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
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.