Let’s write a readable code…does less code mean better performance?

Ishwar Rimal
4 min readJan 19, 2020

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

Photo by John Barkiple on Unsplash

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 it smaller and sophisticated in the 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 optimization techniques for the language..for example, do-while is considered performant over while..but we fail to understand that performance improvement it achieves is at the 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 optimization 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 optimized, 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 was lots of agreement and disagreement from the experienced people. Here is a list of points that I’ve understood and think it’ll be useful for you.

  1. Readability is subjective: What I feel readable might not be the same for others. here is a good thread on it.

2. It’s unnecessary to oversimplify things: though my example was clear, people had an 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.

I 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 clap button. As the saying goes, ‘When we give cheerfully and accept gratefully, everyone is blessed’.

--

--

Ishwar Rimal

Senior FrontEnd Engineer at Intuit. 8 years experience. I write articles on JavaScript, React, Web Optimisation, Startups, Work Life Balance, etc. ❤️ JavaScrip