Chapter 5

KISS and YAGNI in Practice

Suppose the developer assumes in advance that discounts will one day load from a database, a file, an external API, and an admin panel. They build a general rules engine, though only one condition exists right now.

TypeScript
interface DiscountProvider {
  getRules(context): Rule[];
}

class DiscountProviderFactory {
  create(source) {
    if (source === "database") { /* ... */ }
    if (source === "remote") { /* ... */ }
    if (source === "config") { /* ... */ }
  }
}

// All three variants are still imaginary

Formally the architecture looks flexible. Actually the project now has an interface, a registry, a factory, and a config format. Every new team member first studies the mechanism, and only then the simple discount rule. What KISS proposes: keep the rule an explicit function with a clear name, input, and result. To check the discount, opening one file is enough.

TypeScript
function calculateDiscount(subtotal) {
  if (subtotal <= 10000) {
    return 0;
  }

  return subtotal * 0.1;
}

What YAGNI adds: don't create several rule sources until at least a second real source appears. And the solution mustn't block a future change - a simple function fits both conditions at once.

The difference is convenient to keep as a table of signals: the same future change can be prepared soundly or prematurely.

Signal · Sound preparation · Premature architecture

  • A future change - Isolate the formula in a named function; Build a platform for unknown variants
  • Testing - Check the function with a few values; Mock four layers for one formula
  • Extension - Change the structure after the second real scenario; Design every possible scenario in advance
KISS and YAGNI count decisions, not lines. A simple function and a flexible engine can be the same length. The difference is how many decisions and layers you must hold in your head to change one formula.

While rules are few, duplication isn't a problem. But as soon as similar code appears in a second place, DRY comes in - and here it's important to understand it correctly.

Links