Code Smell 20: Premature Optimization

Code Smell 20: Premature Optimization

Maxi Contieri
·Nov 8, 2020·

2 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Planning ahead of time needs a crystal ball no developer has.

TL;DR: Don't guess things that might not happen.


  • Coupling

  • Testability

  • Readability



  1. Create great models and bijections first.

  2. Create a conclusive benchmark once the model is working.

  3. Programmers waste enormous amounts of time 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. Donald Knuth

  4. Design for Performance.

  5. Use Test Driven Development technique. It always favors the simplest solution.


  • Weird data structures

  • Caches

  • Singletons

Sample Code


class Person {
    ancestors() {
        cachedResults = GlobalPeopleSingletonCache.getInstance().relativesCache(;
        if (cachedResults != null) {
            return (cachedResults.hashFor(;
        return database().getAllParents(;
class Person {   
     return this.mother.meAndAncerstors().concat(this.father.meAndAncerstors());      
     return this.ancestors().push(this);


This is a design smell so it can not be detected by mechanical tools (yet).


  • Premature Optimization

  • Antipattern


Defer performance decisions until functional models are mature enough.

Donald Knuth created/compiled the best/fastest algorithms and data structures. With great wisdom he warned us of abuse. Why do we think we are smarter than him?


More info


Photo by Markus Spiske on Unsplash

Premature optimization is the root of all evil.

Donald Knuth

This article is part of the CodeSmell Series.

Last update: 2021/07/02

Share this


Technical Opinions are my own. I don't have the revealed truth.

Software Design is a creative activity. These are hints and not rigid rules.

I write on BackEnd Business Systems and OOP Design. My advice/experience might not suit other systems.

You can write me at info(at)