Skip to content
Velloreth
Programming 7 min read

DRY, KISS and YAGNI: Three Principles, One Goal

DRY, KISS and YAGNI are three heuristics for simpler code. Learn what each really means, how they conflict, and how to apply them sensibly.

By

Three heuristics, one goal

DRY, KISS, and YAGNI are three ways of saying the same thing: keep the code simple enough to change.

They approach it from different angles. DRY targets duplicated knowledge. KISS targets unnecessary complexity. YAGNI targets speculative work. Together they form a useful set of checks, and they also conflict, which is where the interesting judgement lies.

DRY: Don’t Repeat Yourself

The principle, from The Pragmatic Programmer, is:

Every piece of knowledge must have a single, authoritative representation within a system.

The key word is knowledge. DRY is not about identical text. It is about a single fact being expressed in multiple places, so that changing the fact requires changing every copy.

// Violates DRY: the tax rate is knowledge, duplicated
function priceWithTax(price: number) { return price * 1.13; }
function invoiceTotal(subtotal: number) { return subtotal * 1.13; }

The tax rate is a fact. It appears twice. When the rate changes, one copy will be missed.

// Follows DRY
const TAX_RATE = 1.13;
function priceWithTax(price: number) { return price * TAX_RATE; }
function invoiceTotal(subtotal: number) { return subtotal * TAX_RATE; }

Now the fact has one home.

The misuse: merging coincidental similarity

The most common DRY mistake is merging code that looks the same but represents different knowledge.

// Two rules that happen to look identical today
function validateUsername(name: string) {
  return name.length >= 3 && name.length <= 20;
}

function validateDisplayName(name: string) {
  return name.length >= 3 && name.length <= 20;
}

These are identical. Merging them seems obviously right.

But they are different rules. Usernames might later be restricted to alphanumeric characters; display names might allow emoji. If you merge them, the next change to one breaks the other, and you have to un-merge.

The test is: do these change for the same reason? If yes, merge. If no, leave them separate even though they look alike.

This is why the rule of three exists. Two occurrences might be coincidence. Three suggests a real pattern.

KISS: Keep It Simple

Complexity is the primary cost of software. It makes code harder to read, harder to change, and harder to debug. KISS says: prefer the simplest thing that works.

// Over-engineered
class StringReverserFactory {
  create(): StringReverser { return new StringReverser(); }
}

class StringReverser {
  reverse(input: string): string {
    return input.split('').reverse().join('');
  }
}

// Simple
function reverse(input: string): string {
  return input.split('').reverse().join('');
}

The first version has three types to do what one function does. It is not more flexible in any way that matters — there is one reversal algorithm and there will only ever be one.

What complexity looks like

Complexity is not just long code. It shows up as:

  • Indirection — layers that only forward calls.
  • Configuration — options nobody sets.
  • Abstraction — interfaces with one implementation.
  • Generality — handling cases that cannot occur.
  • Premature optimisation — caching before measuring.

Each of these has a cost: more to read, more to understand, more to maintain. The benefit must exceed that cost.

The counterweight

KISS is not an argument against all abstraction. Abstraction that removes real duplication or isolates a real boundary is a simplification, not a complication.

The test: does this abstraction make the code easier to understand and change? If yes, it is simple. If it adds a layer without removing a problem, it is not.

YAGNI: You Aren’t Gonna Need It

YAGNI says: do not build for requirements you do not have.

// YAGNI violation: a plugin system for one plugin
interface FormatterPlugin {
  name: string;
  format(data: unknown): string;
}

class PluginRegistry {
  private plugins = new Map<string, FormatterPlugin>();
  register(plugin: FormatterPlugin) { /* ... */ }
  get(name: string): FormatterPlugin { /* ... */ }
}

// What was actually needed
function formatJson(data: unknown): string {
  return JSON.stringify(data, null, 2);
}

The plugin system is speculative. It assumes there will be multiple formatters, that they need to be registered dynamically, and that the name matters. If none of that is true, it is pure cost.

Why speculative generality fails

The guess is usually wrong. You design for a requirement that never arrives, or arrives in a different shape than you imagined.

It costs now for a benefit that may never come. The abstraction must be written, tested, documented, and maintained.

It makes the real change harder. When the actual requirement arrives, the speculative abstraction often does not fit, and you must refactor it away first.

It obscures the current behaviour. Code that handles hypothetical cases is harder to read than code that handles the real one.

The counterweight

YAGNI is not an argument against designing for known requirements. If you know the second payment provider is coming next month, building the interface now is not speculative — it is planning.

The distinction is between known and imagined requirements. YAGNI targets the imagined ones.

How the three conflict

The principles pull against each other, and recognising the tension is the useful part.

DRY versus YAGNI. DRY says extract the shared logic. YAGNI says do not build the abstraction until you have a second case. The resolution is the rule of three: tolerate duplication until the pattern is clear.

DRY versus KISS. DRY says merge the duplicates. KISS says the merged version may be more complex than the two originals. The resolution is to check whether the merged version is genuinely simpler, or just shorter.

KISS versus YAGNI. These usually agree — both push toward less code. They conflict when a simple solution requires a small amount of speculative structure, such as a configuration flag.

A worked example

Suppose you have three functions that each fetch data from an API and cache it.

async function getUsers() {
  const cached = cache.get('users');
  if (cached) return cached;
  const data = await api.get('/users');
  cache.set('users', data);
  return data;
}

async function getPosts() {
  const cached = cache.get('posts');
  if (cached) return cached;
  const data = await api.get('/posts');
  cache.set('posts', data);
  return data;
}

DRY says: extract a cachedFetch(key, path) helper. The caching logic is knowledge, and it is duplicated.

YAGNI says: you have two cases, and the caching policy might differ per resource. Wait.

KISS says: the helper is genuinely simpler than the duplication, and the caching rule is one rule.

Here the three agree: extract the helper. The caching logic is a single piece of knowledge, it changes for one reason, and the extraction reduces the total code.

Now suppose the two functions differ: users are cached for an hour, posts for a minute.

async function getUsers() {
  const cached = cache.get('users');
  if (cached && Date.now() - cached.at < 3600_000) return cached.data;
  // ...
}

Now the caching policy differs. Extracting a shared helper requires a TTL parameter, and the helper becomes a general-purpose cache with configuration. That is more complex than the two originals.

Here DRY and KISS conflict, and KISS wins: keep them separate, or extract only the part that is genuinely shared.

Practical guidance

  • Apply DRY to knowledge, not to text. Ask whether the two copies change for the same reason.
  • Use the rule of three before extracting an abstraction.
  • Prefer the simplest thing that works, and add complexity only when a real problem demands it.
  • Do not build for imagined requirements. Build for known ones.
  • Check whether an abstraction simplifies or just relocates complexity.
  • Accept some duplication when merging would couple unrelated things.
  • Revisit the decision when the third case appears.

For the related principles, see SOLID principles explained and refactoring basics. For the practical application, technical debt conversation and writing clean functions.

Quotable summary

DRY says every piece of knowledge should have a single authoritative representation, and it applies to knowledge rather than to identical-looking text, so two functions that look alike but change for different reasons should stay separate. KISS says prefer the simplest solution that works, because complexity is the main cost of software. YAGNI says do not build for requirements you do not have, because speculative generality usually goes unused and makes the real change harder. The three conflict: DRY pushes toward abstraction, YAGNI pushes against it, and KISS arbitrates by asking whether the abstraction genuinely simplifies. The rule of three — tolerate duplication twice, refactor on the third occurrence — resolves most of the tension.

Frequently asked questions

What does DRY stand for?
Don't Repeat Yourself. It means every piece of knowledge in a system should have a single, authoritative representation, so a change to that knowledge requires changing one place.
What is the difference between DRY and avoiding duplicate code?
DRY is about duplicated knowledge, not duplicated text. Two functions can look identical but represent different business rules that will change independently, in which case merging them violates DRY in spirit.
What does KISS mean?
Keep It Simple, Stupid. It means preferring the simplest design that solves the problem, because complexity is what makes software expensive to change and hard to understand.
What does YAGNI mean?
You Aren't Gonna Need It. It means not building features, abstractions, or configuration for requirements you do not have, because speculative work usually goes unused and adds maintenance cost.
Do DRY and YAGNI contradict each other?
They pull in opposite directions. DRY encourages extracting shared abstractions; YAGNI discourages building abstractions before you have a second real case. The resolution is to wait for the second or third occurrence before abstracting.
What is the rule of three?
The rule of three says you should tolerate duplication twice and refactor on the third occurrence. By then you have enough examples to see the real abstraction rather than guessing at it.