Part 5 of 6 · 2 min read
YAGNI
Do not build for a future that may not come.
YAGNI: you aren't going to need it
YAGNI means "you aren't going to need it". Do not write code for a need that nobody has stated. An unused interface, a configuration option that nobody sets, a plug-in system for one plug-in: each must be read, tested and maintained, and each may be wrong when the need finally arrives.
CA treats this fairly. It says that over-engineering is often much worse than under-engineering. It also admits the other side: when a real boundary turns out to be needed where none exists, adding it late can be costly and risky. So its advice to architects is to judge, to guess carefully, and to review that guess again as the project moves.
CA's compromise at the large scale is a partial boundary. Do the work to separate two parts so that they could be separate, but keep them together in one deployable piece for now. At the class level, the same idea is a small interface that costs one line to write, kept at a spot where change is likely, such as payment or storage.
A way to decide is to ask three questions.
- Has someone asked for this, or is it a guess?
- If it is needed later, how hard is it to add then?
- If it is wrong, how hard is it to remove?
Add it now only when the answer to the first is yes, or when adding later would be very expensive and the risk is real. Otherwise write the simple version and leave a note.
Interview angle
Answer in your head first, then open it.
What are DRY, KISS and YAGNI?
DRY: each fact lives in one place. KISS: pick the simplest design that works. YAGNI: do not build for needs nobody has stated.
Do these ever conflict with SOLID?
Yes. Interfaces and splits add structure. Use SOLID where change is likely, and keep the rest simple.
How do you decide how much to design up front?
Ask what is stated, what is cheap to add later, and what is costly to reverse. Say it out loud.
Check yourself: 1. What does CA say is often worse than under-engineering? 2. What is a partial boundary? 3. Which three questions help decide?
Words you will meet
| Word | Meaning |
|---|---|
| DRY | Each fact or rule is written in one place |
| KISS | Choose the simplest design that meets the need |
| YAGNI | Do not build features nobody has asked for |
| Over-engineering | Adding structure that the problem does not need |
| Rule of three | Remove duplication when it appears the third time |