The Perfect Code Paradox
I have been thinking on writing this article for a while now, mostly because an experience I had recently on StackOverflow, and also because I have been seeing this "effect" on a lot of my software developers friends and technical sites.
The concept of optimal code or "perfect code" has been around for a while, and its has become the Holy Grail of every software developer..milliseconds optimizations have become the proof of supremacy as a software developer..but, is really worth it? what is the price of the "perfect code"?
Recently I made a contribution to SO for a question I saw. The person was offering 50 points for a solution, so I gladly jumped and offered a solution. To be honest I didn't put too much effort, it was a simple logical problem, so I just came up with a simple working solution. (See the end of the article for the problem).
So I wrote the user my idea, and almost instantly I received a comment of another contributor who criticized my idea because there was a more optimal (and complex) way to do it, using math etc. At that point I agreed but I mentioned something of which many software developers don't take into account in a daily basis. Maintainability and Legibility. He didn't liked my response.
During a Company development process many developers take part into the creation process. Some of them, more talented or experienced than others. Some stay, some part ways looking for better opportunities, leaving behind the produce of their skills. And then a newcomer takes that code and try to "figure it out", and it does not matter how skilled you are sometimes your find yourself in a maze of code that make no sense at all to you, even when it works.
The possible two solutions here are: spend a couple of weeks trying to see what was on that developer mind when he created that monster, or, spend a couple of weeks writing your own monster..
Guess who loses here..
Yes, the company..
So basically the "perfect code" comes with a cost. And a high one. And most of us go home thinking "well, it's not my fault the company hired that crazy guy.." but we end up doing exactly the same thing, and even sometimes the same monster we created comes back a couple of moths later to bite our behinds, adding costs to the task.
That's why having a maintainable and readable code base its so critical. Code that can be easily read and maintained for any reasonably skilled developer, junior or senior. That way the code quality will increase with every interaction, since the developers will not have to spent hours debugging to understand what a function does, or does not. It will be easier to find out what is wrong, and subsequently much easier and faster to correct it.
Many years ago, when saving bytes or even bits was a matter of life or death, extreme optimizations were the only way to go. On today's world, processing and storage capabilities exceed by far our most wildest dreams. So, if you are not designing a critical process for a very limited environment, don't sink into complexity with no purpose. Being a software developer is also to be productive, to meet time expectations and to make other's (and yourself) life easier. For the "perfect code" there will be always another opportunity, in the mean time, don’t let the perfect become the enemy of the practical.
PS: The problem was as follow:
Given an array of integers, a sum of the digits of each element is needed as a result:
Example:
Array:
9 23 45 65 345 2
Expected result:
9 5 9 11 12 2
Exactly my point!...question is...when is too much?
As Don Knuth said: “Premature optimization is the root of all evil”
"Good enough is perfect." -- PJ Flauger This one of my all-time favorite sayings. It applies to coding as well as to life.
Sure! Optimization is always a good practice..however, what most of us don't have is the sense of optimization..when to do it, when is necessary...the real danger is going for optimization BEFORE is needed.. :)
A good programmer knows when and where to optimize. If it's just a very specialized operation and a blocking operation then extreme optimization is acceptable. Years ago I saw a library that mixes c and assembly code to fine tune the speed,.and the assembly code is well documented. What I'm saying is that, one can go to extreme in optimization but it has to be well documented even to help the author in case he needs to came back again to that code.