Is premature optimization really the root of all evil?
Tags: softwaredesign, softwareengineering, softwaredevelopment, optimization
Published: 2021-01-06 09:11:27

---
title: Is premature optimization really the root of all evil?
published: true
date: 2021-01-06 08:37:47 UTC
tags: softwaredesign,softwareengineering,softwaredevelopment,optimization
canonical_url: https://medium.com/@dvirsegal/is-premature-optimization-really-the-root-of-all-evil-50315acdf359
---

![](https://cdn-images-1.medium.com/max/1024/0*Fi1YzQoylzDTEBBH)<figcaption>Photo by <a href="https://unsplash.com/@eilisgarvey?utm_source=medium&amp;utm_medium=referral">Eilis Garvey</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption>

#### First, make it right. Then make it fast.

![](https://cdn-images-1.medium.com/max/600/0*Rpzv4eSwg2GmgJoM.jpg)<figcaption>Premature Optimization by <a href="https://medium.com/u/a4a05e49e068">Leo Polovets</a> — <a href="https://i0.wp.com/www.clarkdever.com/wp-content/uploads/2019/03/C1NuQ8NWIAAAEAA.jpg?w=600&amp;ssl=1">src</a></figcaption>

Back in the seventies, Knuth wrote the above quote in his paper “[Structured Programming with go to Statement](https://dl.acm.org/doi/abs/10.1145/356635.356640)” at times when computational power was slower and expensive than nowadays. Nevertheless, it doesn’t mean you shouldn’t take it in your consideration. Note that the quote doesn't stand for itself, and to get its true meaning, you should read the paragraph that follows this famous saying:

> Yet we should not pass up our opportunities in that critical 3%. A good programmer will not be lulled into complacency by such reasoning, he will be wise to look carefully at the critical code; but only after that code has been identified. It is often a mistake to make a priori judgments about what parts of a program are really critical, since the universal experience of programmers who have been using measurement tools has been that their intuitive guesses fail.

![](https://media.giphy.com/media/9l6D0qQALI7zXaBzHs/giphy-downsized-large.gif)<figcaption>Good Measure — [src](https://media.giphy.com/media/9l6D0qQALI7zXaBzHs/giphy-downsized-large.gif)</figcaption>

To me, what it really means, there are obvious optimizations (like not doing string concatenation inside a loop). Still, anything that isn’t a trivially clear optimization should be avoided until it can be **measured**. Failed to do so can introduce unexpected bugs and can result in high efforts to overcome them.

That being said, measurements can lie. Focusing on hot-paths and optimizing them might cause you to miss that it is a symptom of poorly designed code. There is a simple solution: be sure to **design first**. Thus, a more holistic approach will be finding those places that don’t correlate with your design and adjust them accordingly.

Note that until you‘ve written code that you can measure, you’ll have to make some performance decisions before anything exists. Sometimes these decisions are difficult to change if you get them wrong. For this reason, it’s good to have a general idea of what things cost, so you can make reasonable decisions when no hard data is available.

![](https://cdn-images-1.medium.com/max/284/0*HHXKvXzYdfVn_M50.png)<figcaption>Premature Optimization — <a href="https://xkcd.com/1691/">src</a></figcaption>

Furthermore, I saw cases that premature optimization was used (or abused) as an argument for not designing your entire application architecture to run fast in general, scale, and be easily optimizable—basically, an excuse not to optimize code at all. Don’t be confused. Designing a solution is your engineering responsibility.

How early to optimize and how much to worry about performance depends on the task. When writing a script that runs a few times and only by you, I would worry less about performance. But if you work on a service with many users and will be used in different ways — be sure to optimize to cover all the diverse use cases efficiently.

However, a balance must be struck between performance and readability, maintainability, elegance, extensibility, and so on.

**To Wrap up**

Slow performance can’t be blamed on the language or tool or CPU or memory. It’s a delicate balance of so many things, which is why it’s nearly impossible to optimize early.

The developer has to balance between design and optimization. To my understanding, he should design first, code, and profile it to see which parts should be optimized, and do it iteratively. [Here’s a great example](https://stackoverflow.com/questions/926266/performance-optimization-strategies-of-last-resort/927773#927773).

A simple and elegant design is often easier to optimize at these earlier stages, and profiling may reveal unexpected performance problems that would not have been addressed by premature optimization.