Premature optimization involves trying to improve something when it’s too early to do so. For example, a software developer could engage in premature optimization by spending a lot of time perfecting a certain piece of code, even though it’s unclear whether this code will actually be needed. Similarly, someone could engage in premature optimization by spending a lot of money on the best equipment available for a certain hobby, even though they barely started with the hobby, so they don’t need that equipment yet and might end up never using it.
Premature optimization can lead to various issues, such as wasting resources and increasing commitment to a problematic course of action, so it’s important to understand it. Accordingly, in the following article you will learn more about this concept, and see how you can avoid optimizing things prematurely, while still recognizing situations where optimization is appropriate.
Examples of premature optimization
People engage in premature optimization in many ways and in many areas of life. This includes, for example:
- Spending a lot of resources trying to optimize certain functions in a codebase early on, even though these optimizations are likely to be irrelevant later, due to changes in the code.
- Spending a lot of resources trying to structure a startup in a way that will allow it to scale to hundreds of millions of users, before having acquired even a single one.
- Spending a lot of resources incorporating a company legally, before checking that anyone is interested in the product that it will sell.
- Spending a lot of resources picking out the best possible gear for a hobby, before actually starting the hobby.
Note that, in some of these cases, whether a certain optimization is considered premature or not depends on factors such as the specific reason why it’s done. For example, if someone wants to start exercising as a hobby, and they know that having good gear will motivate them to stick with it, and they can also easily afford to spend the necessary resources, then the optimization might be considered reasonable, rather than premature.
The origin of premature optimization
The concept of premature optimization was first made prominent in the field of software engineering. It is attributed to Sir Tony Hoare, though it was popularized by Donald E. Knuth, in the following quote:
“There is no doubt that the holy grail of efficiency leads to abuse. Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.
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.”
— From “Structured Programming with go to Statements” (Knuth, 1974)
It is important to read the full statement carefully, because it not only cautions against premature optimization, but also acknowledges that it’s nevertheless important to identify areas where optimizations are necessary, and then implement those optimizations.
In addition, there are other concepts that are closely related to this. One such key concept is you aren’t gonna need it (YAGNI), a software-development principle which denotes that you should “Always implement things when you actually need them, never when you just foresee that you need them”.
The dangers of premature optimization
Premature optimization can lead to various issues, such as:
- Wasted resources, for example if you spend time, effort, and money doing something that turns out to be unnecessary.
- Increased mistakes, for example if you make decisions at a stage where you don’t have enough information.
- Worse outcomes, for example if you feel locked into a sub-optimal course of action because you want to benefit from your early optimization.
- Increased negative emotions, for example if you become discouraged or frustrated because all your hard work ended up becoming utterly irrelevant.
Causes of premature optimization
People engage in premature optimization for many reasons, including the following:
- It lets them focus on things that are relatively easy for them to handle. For example, if someone wants to start a new hobby, such as rock climbing or guitar playing, then they might look at expensive gear and outline a long-term training plan before they even start the hobby, because doing this is easier, more fun, and less scary than actually getting started.
- It serves as a way for them to fantasize about the future. For example, if someone has an idea for an app, but they’re not sure how to develop it, then they might focus on things that aren’t needed yet, such as finding prestigious competitions that they will be able to apply to, because this gives them a way to feel that their app will be successful.
- They fail to prioritize properly. For example, someone who is developing software might optimize things prematurely simply don’t know how to plan ahead and figure out which tasks they should be working on during each stage of the development process.
In addition, people can engage in premature optimization for other reasons, such as social factors. For example, they may do it because they want to fit in with other people’s behavior, or because they’re afraid of missing out on something. Alternatively, they may need to look like they’re working on something (e.g., because that’s what their boss expects), even if it would be better to wait before working on it.
Finally, premature optimization can also be driven by associated cognitive phenomena. For example, it can be driven by the action bias, when people feel the need to act, precrastination, when people rush to do things too early to feel satisfaction, and procrastination, when people optimize things prematurely as a way to delay working on other things. Furthermore, premature optimization can coincide with other phenomena, such as bikeshedding, a phenomenon where people spend a disproportionate amount of resources dealing with relatively minor issues.
How to avoid premature optimization
To avoid premature optimization, you should assess situations before deciding whether and how to optimize. In doing this, you should consider the following:
- Why do you want to optimize? For example, do you believe that this optimization will actually have substantial positive impact, or are you just focusing on it to delay dealing with something else?
- What are the benefits of optimizing? Specifically, what do you stand to gain from making this optimization, in both the short term and the long term?
- What are the costs of optimizing? Specifically, what resources will you need to spend to make this optimization (e.g., time and money), and how much of them will you need to spend?
- What else could you be doing? For example, is there something more beneficial that you could be doing instead of making this optimization? Essentially, this involves identifying the opportunity cost of this optimization—the value of the other things that you could be doing.
- What are the possible dangers of optimizing? For example, could it lead you to commit to a sub-optimal course of action because you don’t yet have enough information?
- How likely is it that this optimization will become irrelevant? An optimization can become irrelevant for many reasons, such as that you decided to forego the original plan of action associated with the optimization. Note that just because something might become irrelevant eventually doesn’t mean that you shouldn’t work on it now, but the possibility that this will happen is something that you should consider when deciding whether to optimize. In doing so, you should consider associated factors, including the time it will take before the optimization will become irrelevant, and the benefits you will receive from this optimization before that happens.
- What are the advantages and disadvantages of waiting with this optimization? For example, is it possible that there are no downsides to waiting with this optimization until a week from now, at which point you will have more available information?
However, when doing this, it’s important to remember that not all optimization is premature. This means that rather than avoiding all optimization, you should make sure to assess the situation before deciding whether to optimize something or not. For example, even if the optimization occurs at an early stage, you might decide that it’s worthwhile, because the costs of implementing it are low, and its benefits are very substantial.
This caveat is noted in the original quote that popularized the concept of premature optimization. Specifically, people often focus only on the part of the quote where it’s said that “premature optimization is the root of all evil”. However, the full quote actually says that “We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%”. (Note: The key takeaway from this is that there are some cases where optimizations are worthwhile, rather than that there are exactly 3% of cases where this is worthwhile, as the exact portion of cases depends on many factors.)
In summary, to avoid premature optimization, you should assess situations before deciding whether and how to optimize. In doing this, you should consider factors such as why you want to optimize, what are the costs and dangers of optimizing, and what else you could be doing. However, you should also keep in mind that not all optimization is premature, and there are cases where you should optimize things.
Note: A useful and related concept is the 80/20 rule (also known as the Pareto Principle), which in this case can denote that 80% of the positive outcomes that you experience will come from 20% of the work that you do.
Summary and conclusions
- Premature optimization involves trying to improve something when it’s too early to do so, for example by spending a lot of time perfecting a certain piece of software code, even though it’s unclear whether this code will actually be needed.
- Premature optimization can cause various issues, such as wasted resources, increased mistakes, worse outcomes, and increased negative emotions.
- People optimize prematurely for many reasons, including that this lets them focus on things that are relatively easy for them to handle, that they fail to prioritize properly, and that they want to fit in with other people’s behavior.
- To avoid premature optimization, you should assess situations before deciding whether and how to optimize, by considering factors such as why you want to optimize, what are the costs and dangers of optimizing, and what else you could be doing.
- Remember that not all optimization is premature, so this doesn’t mean that you should always avoid optimizing, but rather that you should make sure to determine whether optimizations are worthwhile.