Premature Optimization: What It Is and How to Avoid It

Premature Optimization

 

Premature optimization is the act of trying to make things more efficient at a stage when it is too early to do so. For example, premature optimization could involve someone spending a lot of time and money picking out the best possible gear for a certain hobby, despite the fact that they haven’t actually tried out that hobby to make sure they enjoy it.

Premature optimization can often end up backfiring, and cause you to waste a lot of resources, such as time, money, and effort, while also increasing the likelihood that you will create future problems. Accordingly, understanding what premature optimization is and how to avoid it can be beneficial in many areas of life.

As such, in the following article you will learn more about this concept, and see some beneficial guidelines that will help you figure out when a certain optimization is needed, and when it is premature.

 

Examples of premature optimization

People make the mistake of trying to optimize things prematurely in many areas of life. This includes, for example:

  • Spending a lot of resources trying to optimize certain functions in a codebase early on, despite the fact that those optimizations are likely to be rendered 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 you’ve even finished developing your product or checking whether anyone wants it.
  • Spending a lot of resources picking out the best gear possible for a hobby, before you’ve actually started taking part in it.

As noted above, there are some situations where optimizing things early on might be necessary, and in such situations this sort of optimizations are considered appropriate, rather than premature. However, in most cases, the optimizations described in these examples are premature, and it would be preferable to postpone them until a later stage.

 

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, who said that:

“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.”

Structured Programming with go to Statements (1974)

This presents the argument against trying to make premature optimizations, while at the same time acknowledging that it’s nevertheless important to identify areas where optimizations can be necessary, and to then implement those optimizations.

 

The problem with premature optimization

There are several reasons why premature optimization is problematic:

  • It can cause you to waste resources, such as time, money, and effort, which could be better spent elsewhere.
  • It can discourage or frustrate you, especially if your hard work ends up going to waste later.
  • It can cause you to make mistakes, if you make decisions at a stage where you don’t have enough information.
  • It can hinder your progress in situations where you feel locked into a sub-optimal course of action, because you want to benefit from your premature optimization.

 

Why people optimize prematurely

There are various reasons why people optimize things prematurely:

  • The premature optimization represents a problem that is relatively easy for them to tackle. For example, if someone has an idea for an app, but they’re not sure how to develop it, it can be tempting for them to spend a lot of time thinking about unimportant things that they can handle, such as the logo design, in order to achieve a false sense of progress.
  • The premature optimization serves as a way for them to fantasize about the future. For example, someone who wants to start a new hobby, such as rock climbing or guitar playing, might spend hours picking out advanced gear and planning a future course of action before they even start training, because doing this is fun, and is easier than actually getting started.
  • The premature optimization occurs as a result of failure to prioritize tasks properly. For example, someone who is developing software might optimize things prematurely not because it’s a coping mechanism for dealing with any issues, but rather because they 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.

Note: a related concept which has similar roots is called bikeshedding; this represents a phenomenon where people spend a disproportionate amount of resources dealing with relatively minor issues.

 

How to avoid premature optimization

So far, we saw what premature optimization is, why it’s an issue, and why people are prone to it. Next, you will see what you can do in order to avoid optimizing things prematurely.

Essentially, when figuring out whether or not you should optimize something, there are several factors you should consider, and several important questions that you should ask yourself:

  • Why do you want to optimize? Do you believe that this optimization is actually necessary at this stage, meaning that it will have a notable, positive impact on your work, or are you just focusing on it right now because you’re trying to avoid dealing with something else?
  • What are the benefits of optimizing? What do you stand to gain from making this optimization?
  • What are the costs of optimizing? What sort of resources will you need to spend in order to make this optimization?
  • What are the possible negative consequences of optimizing? What sort of issues can this optimization cause for you in the future?
  • How likely is it that this optimization will become obsolete? Will what you’re doing now be relevant later, or is this optimization likely to be rendered obsolete? Note that just because something might become obsolete later doesn’t mean that you shouldn’t work on it now, but the likelihood that this will happen, the time it will take before it happens, and the benefits you will receive in the meantime are all things that you should take into account when deciding whether or not to optimize.
  • What are the advantages and disadvantages of waiting with this optimization? Are there any downsides to postponing this specific optimization? Will it actually be better for you to deal with it later, perhaps because you will have more relevant information available?
  • What else could you be working on? If you weren’t spending your time and resources on this optimization, what would you be spending them on? If there are alternative things that you could be working on, would you benefit more from working on them instead?

Based on these criteria, you can prioritize the different tasks that you have to complete, and figure out which ones you should work on at which stage, in order to ensure that you avoid making any premature optimizations.

However, note that you don’t have to ask yourself all of these questions each time you evaluate a potential task. This is especially true if a certain ask is relatively minor, since it might take you less time and effort to simply get a trivial 2-minute task done than it is to evaluate it using all of these criteria.

Rather, the important thing is to be aware of these considerations, and use them, at least to some degree, to evaluate tasks when necessary. The larger a task appears to be, based on the resources that it will require or the effects that it will have, the more wary you should be, and the more you should use these criteria to evaluate it.

 

Remember that not all optimization is premature

It’s important to remember that avoiding premature optimization doesn’t mean that you should avoid optimization entirely. Rather, it simply means that you should think carefully before you decide to spend your resources optimizing something.

This is crucial, since people often repeat the idea that “premature optimization is the root of all evil”, without acknowledging the full quote, which states 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%”.

This means that it can be entirely reasonable to assess a situation and decide that you should optimize something, even if it’s at a relatively early stage. This might happen for a variety of reasons, such as because you believe that a small modification could give you a significant benefit, or because the optimization will allow you to deal with a bottleneck in your work, or because avoiding the optimization might lead to significant technical debt later on.

In the original quote on the topic, this concept was said to apply to roughly 3% of cases, but your cutoff for what a valid optimization is can be higher or lower than that. For example, a common guiding principle is the 80/20 rule (also known as the Pareto Principle), which in this case suggests that 80% of the positive outcomes that you experience will come from 20% of the work that you do.

Overall, to make sure that you avoid optimizing things prematurely, you should always assess the situation first, and determine whether or not the intended optimization is necessary at that point in time. However, this approach shouldn’t become an excuse to avoid optimization entirely, but should rather serve as a way to prioritize tasks as effectively as possible.

 

Summary and conclusions

  • Premature optimization is the act of trying to make things more efficient at a stage when it is too early to do so, such as by working on trivial aspects of a business that you’re developing, despite the fact that there are more important tasks for you to handle.
  • Premature optimization is problematic because it can cause you to waste resources, to get discouraged, to act when you don’t have enough information, or to get locked into a sub-optimal course of action.
  • The most common reasons why people optimize things prematurely are that they fail to prioritize their tasks properly, or that the premature optimization represents an easy problem for them to handle, which makes it appealing to take it on even if it’s unnecessary.
  • To avoid optimizing things prematuerly, before getting started you should make sure to ask yourself why you want to optimize, what are the costs and benefits of doing so, what are the possible negative consequences of this optimization, what are the advantages and disadvantages of waiting, and what else you could be working on.
  • Remember that this doesn’t mean that you should avoid optimization entirely, but rather that you should think carefully and assess the situation before deciding to move through with a certain optimization.