Let’s write readable code...does less code mean better performance?
Ishwar Rimal
My life revolves around JavaScript | Passionate about crafting user-centric digital experiences using modern web technologies.
TL;DR I tweeted my view on why we should focus more on readability and not make code sophisticated just for the sake of performance..it started some good debate from experts in this field. Follow it here
I tweeted my view on ‘writing readable code’ and many JS wizards shared their disagreement as well as agreement on the tweet, specifically the example that i gave.
The post was about focusing on making your code more readable rather than making code smaller and sophisticated in hope of achieving better performance (which itself is a false assumption that we make)
In my personal opinion, we often tend to make mistake while learning about optimisation techniques for certain language..for example do-while is considered performant over while..but we fail to understand that performance improvement it achieves is at micro level which will never (unless you’re looping over something for million times) affect our day to day programming life.
With such assumptions in mind, we tend to try and achieve as much sophistication in our code as possible. Sure it makes our code look fancy but at what cost? The cost is of readability. You may be able to understand that fancy inline function call and inline return you do, but the one who is to follow your work later may not be able to understand it.
// fancy? but is it readable? const isPositive = num => num >= 0
To overcome this, the first thing that we need to understand is about the optimisation steps and at what level does it help, if its micro level it’s better to prefer readability over those tiny performance improvements. Also if we look at the SDLC these days, we always have bundling and building step pre production deployment. So the code is going to get optimised, minified with the best possible techniques and algorithms available which will obviously be better than the ones we tried to achieve.
Some of us might argue saying well this is a simple ternary thing, who doesn’t know ternary things. I can tell you that many people find it difficult to understand the code above in first look. Rather if you had written the above piece as follows, it would have been more readable for everyone(including the smart ones)
// naive but readable function isPositive(num){ if(num >= 0){ return true } return false }
Coming to my tweet, there were lots of agreement and disagreement from the experienced people. Here are list of points that I’ve understood and think it’ll be useful for you.
- Readability is subjective: What I feel readable might not be same for others. here is a good thread on it.
2. It’s unnecessary to oversimplify things: though my example was clear, people had opinion on why even the first approach is not suitable
3. More readability doesn’t mean more text.. (but also to remember, less code doesn’t mean better performance)
4. The point I was trying to make
5. Some good people support you.
And lots of other points are covered in the thread.
Hope you found this article useful. I would love to hear your thoughts. ??
Thanks for reading. ??
Cheers! ??
If you find this article useful, you can show your appreciation by clicking on like button. As the saying goes, ‘When we give cheerfully and accept gratefully, everyone is blessed’.