Video in progress

YAGNI and "design open for extension" seem to contradict each other. In practice, how do you decide how much to abstract up front?

I abstract only for the change points that are known now; for a guessed future I keep the code simple but easy to change. For irreversible decisions (API contracts, data schemas, module boundaries) I think much more up front.

📖 Answer

The main difference is the cost of change. In internal code, extracting an interface later is 10 minutes of work in the IDE. Changing a public API, a data format in the database or an event schema takes weeks and a migration. So:

  • Internal code: YAGNI. No interface for a single implementation, no strategy "because we'll need it later", no generic Processor<T, R>. The code is simple and the tests are good, so refactoring later is cheap.
  • Boundaries: careful design. In APIs, use shapes that can be extended (a parameter object, a string code instead of an enum plus tolerance for unknown values), and a version field in the schema.

Signs of speculative generality:

  • an abstract class or interface has had a single implementation for a year;
  • "plug-in points" in the configuration that have never changed;
  • a type parameter that is only ever called with one value;
  • a new developer has to open 6 files for a simple change.

When deciding, I look at history, not at fantasies about the future: how many times, and for what reasons, has this spot changed in the last six months? If payment providers get added every quarter, then yes, a strategy. If the export format has been only CSV for 3 years, a plain class without any if is enough.

One more practical rule: introduce the abstraction when the second real case shows up, not the first. With two concrete examples you can see what is common and what differs; an abstraction built from one example usually splits in the wrong place and later turns into the "wrong abstraction" problem above.

And always consider the opposite direction too: removing an abstraction that is no longer used is also YAGNI.