No, really: YAGNI

Open on Substack · Markdown

Friday evening I had dinner with Felix. Among other things, we talked about good code. Good code, we both agreed, is simple. It's code boiled down to its essence.

"Perfection is achieved when there's nothing left to take away", Felix half-quoted Antoine de Saint-Exupéry. 

Good code, he said, looks as if it had been rewritten five times and the author learned with each iteration what's important and what isn't, what the essence of the code is and removing what isn't.

Not only do I agree, but I also think this is yet another exclamation point after YAGNI ("You aren't gonna need it").

YAGNI, I truly believe, is one of the most important principles required to write good code. YAGNI, to me, means that you don't clutter your code with optionality for a tomorrow that might never come.

Don't add code because "in the future we might …” Even if. Even if the intentions are good, even if the code would be neat, even if, yes, should the day come that we need to solve this problem, this code would solve it well — even then: don't add it.

You can't predict the future, no one can. What looks probable today might look laughable tomorrow and code added based on what's likely today might be dead code in a few months.

Until then it's zombie code: code that isn't strictly dead — it is executed — but it doesn’t affect functionality. It’s there because it might affect functionality some day. And code — dead or alive or something in between — ain't free: it needs to be read, maintained, its tests need run. The less we have, the better off we are.

If we can we remove it, let’s remove it, and if we know we could remove today, let’s not add it.

Joy & Curiosity