How to organize code: the perfect trio
Three principles that seem simple, but take time to balance in practice:
- YAGNI — You Ain’t Gonna Need This
- KISS — Keep It Simple, Stupid
- DRY — Don’t Repeat Yourself
KISS: Keep It Simple
The message is straightforward: keep it simple. If it’s working, leave it. If it’s delivering what’s needed, leave it. If it was done in the simplest possible way, that’s probably it.
It’s worth resisting the temptation to add complexity where it wasn’t requested. It’s very easy to start writing code thinking “someone might need this someday in the future” — but most of the time that day never comes.
YAGNI: You Ain’t Gonna Need This
With KISS in mind, it’s worth thinking twice before turning something into a component or sharing business rules without a clear need to do so.
- Creating a function for something you’ll only use once can be premature
- The same goes for interfaces and classes created “just in case”
If you don’t need it now, it’s probably not worth creating yet.
DRY: Don’t Repeat Yourself
YAGNI’s antagonist — and that’s why I talked about it first. If you already have a function that does something and need to do the same thing elsewhere, reuse it. But only if you’re confident that this function won’t change in the future.
And if it does change? The temptation will be this:
“We can build some logic where this component becomes a middleware that takes parameters and returns a Higher Order Function…”
It’s usually worth resisting that path. KISS will thank you.
The internal struggle
Your career as a junior and early mid-level developer will be a constant struggle between these three principles. To find the balance, reading other people’s code helps a lot — and so does participating in discussions about the subject, when possible.
Your brain will find patterns and start applying these principles naturally. If someone around you still doesn’t know this trio, that makes sense — it’s not a subject that’s discussed all that much. Everyone gets there in their own time.
Extra tips
Well-written code is code that’s easy to read, easy to understand, and easy to maintain.
- Easy to read for someone who doesn’t know the codebase
- Easy to maintain for someone who does
And something that helps a lot in the long run: keeping the code consistent with the rest of the codebase. When everyone follows a similar approach, it becomes much easier for anyone to come in and understand what was done.