I love efficient code. There is an odd type of satisfaction to writing well-performing code. Faster! Shorter! Less memory-consuming! More power! MORE!
Well, it’s easy to see how quickly this gets out of hand. Before you know it, your code is a hot mess of high-performing, totally unmaintainable blocks. What started as a beauty ends up being hideous.
There is an extremely important guiding principle I learned to appreciate and adopt (almost religiously!):
Keep it simple, stupid (K. I. S. S). Always prefer clear code to optimal one.
Once your code works, see if it requires any optimisation. You may be surprised to find it doesn’t. Your easy to read, easy to maintain code may be good enough.
Keep your heavy guns for when you encounter performance issues. Try to contain the “smart” bits. Extract them to a clearly-named function. Assign them to a well-named variable. Don’t let optimisations destroy the readability and maintainability of your code.
And always, always opt for the simplest solution.
Lastly, I have to say this rule seems to apply to everything else in life. Don’t over-complicate things. In most cases, you will find the simplest explanation is the right one. The simplest way to perform a task is also the easiest.
Simple is easy. It’s easy to remember. It’s easy to repeat. It’s easy to explain. It’s… Just that simple.