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.
If you enjoyed this message, subscribe to The Daily Commit to get future messages to your inbox.
Top comments (0)