Readability vs performance

November 11, 2021

Whenever someone makes the claim that code readability is paramount, someone in the audience has a knee-jerk reaction against the idea on the grounds of performance.

Such reactions are misplaced.

Are there times when performance is more important than readability? Yes. Absolutely.

Should that be your starting point? No. Absolutely not!

Write your code to be readable and maintainable.

If you observe that your code is not efficient enough, do some benchmarks, and see if you can improve the desired performance charactaristics. Most of the time this can be done without hurting readability at all. Often, the best way to improve performance is to simplify the code, which may also improve readability.

If you still find that your code is not efficient enough, then, and only then, refactor for performance over readability, and use plenty of comments. Remember: comments are like apologies to the next programmer. And when you’ve intentionally sacrificed readability, it’s appropriate to apologize.


Related Content

Readability is paramount

What value is short, clever code that nobody understands? Readability should usually be the first priority in any technical artifact you write.

Is it a good day to optimize?

How do you decide between watching James Bond or a romantic comedy? Every decision optimizes for something. What do you optimize for when designing computing systems?

When is a shortcut appropriate?

The only time less typing is more important than readability is on the command line. When writing code, readability wins by a mile.