Code readability is always important, but in some applications speed is even more important. Depending on the use case of your software you might need to prioritize one over the other.
But there is one important fact:
software developers are mostly reading the code and not writing the code.
So any decrease in readability is slowing down the entire project.
But sometimes that is unavoidable. Because what’s it worth to you if the code is very readable and the project was finished in record time, when it’s running too slow and your users hate it(?)
When Is Speed Important?
GAMES
Most common speed situation everyone comes across are games. And I don’t know anyone who likes slow and low frame rate games.
So if you put all your effort into finishing your project as fast as possible (which is possible because your code is super readable) your users will not be happy.
They want speed.
Below is an example of fast inverse square root from Quake. And even after a decade of being a software developer – I have no idea what this does.
But who cares if the players got an amazing gaming experience.
float Q_rsqrt( float number )
{
long i;
float x2, y;
const float threehalfs = 1.5F;
x2 = number * 0.5F;
y = number;
i = * ( long * ) &y;
i = 0x5f3759df - ( i >> 1 );
y = * ( float * ) &i;
y = y * ( threehalfs - ( x2 * y * y ) );
return y;
}
MINING MACHINES
Another place where the speed is very important is crypto mining machines.
The only goal for this device is to work as fast as possible. And every other goal is secondary.
Especially when you’re paying top dollar to have cutting-edge state-of-the-art hardware. You really don’t want to put some slow software into this perfect machine.
It’s like putting old dirty fuel into a brand new Lambo.
You just don’t do it.
COMPUTER-RELATED
Drivers, operational systems, or processor microcode – it all has to work fast.
And one thing you’ll notice is every system that requires speed doesn’t use JavaScript. It’s always C, C++, or some other lower level language.
So as a rule of thumb:
- if you write in C or C++ then the speed is a priority
- if you write in JavaScript then it’s not
When Is Readability Important?
Most popular form of software development today is web development. And usually you only care about:
- ability to launch products as fast as possible
- enable collaboration of large number of people on a single project
- quick onboarding of new people on the project
And what helps to achieve all of these goals?
Code readability!
SPEED BOTTLENECK
When developing software it’s important to identify where the majority of delays come from.
In web development it’s latency:
- waiting for server to respond
- waiting for database to be read
- waiting for bits to come from a server half a world away
So if we wait 100-200ms for a server to respond, is it really going to matter that we shaved-off 1ms from our JavaScript algorithm?
I don’t think so.
And if you used more time to develop this 1ms saving (on top of making your code less readable) then you failed.
You really achieved nothing except wasted your time and the time of other developers who are going to find it harder to read your code.
Which is going to take more of their time.
IT’S ALL ABOUT PRODUCTIVITY
You should never overengineer a task you have in front of you.
Because software development is an industry. And your task is to find a good enough solution.
I know the words “good enough” sound bad but they are the most appropriate ones.
When you think about it: did you ever written a piece of code that couldn’t be faster?
Probably never.
And yet – everyone was happy about it:
- your boss
- your clients
- your clients’ users
- and even you
And if you decided to focus on making the best code ever – probably none of the above mentioned people wouldn’t be happy.
Instead you did “good enough” job. Everyone was happy. And you got a nice sum of money as your reward.
All because you decided to prioritize readability over speed.
And this is the secret to making more money in this industry: provide people “good enough” solutions they want, and not the “perfect” solutions they don’t want.
P.S. -> read this article if you want to double your salary