DEV Community

Cover image for Code Smell 20 - Premature Optimization
Maxi Contieri
Maxi Contieri

Posted on • Edited on • Originally published at maximilianocontieri.com

1

Code Smell 20 - Premature Optimization

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

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

Problems

  • Coupling

  • Testability

  • Readability

  • YAGNI

Solutions

  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.

Examples

  • Weird data structures

  • Caches

  • Singletons

Sample Code

Wrong

class Person {
    ancestors() {
        cachedResults = GlobalPeopleSingletonCache.getInstance().relativesCache(this.id);
        if (cachedResults != null) {
            return (cachedResults.hashFor(this.id)).getAllParents();
        }
        return database().getAllParents(this.id);
    }
}
Enter fullscreen mode Exit fullscreen mode

Right

class Person {   
  ancestors(){
     return this.mother.meAndAncerstors().concat(this.father.meAndAncerstors());      
  }
  meAndAncerstors(){
     return this.ancestors().push(this);
  }
}
Enter fullscreen mode Exit fullscreen mode

Detection

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

Tags

  • Premature Optimization

  • Antipattern

Conclusion

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?

Relations

More info

Credits

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

AWS Q Developer image

Build your favorite retro game with Amazon Q Developer CLI in the Challenge & win a T-shirt!

Feeling nostalgic? Build Games Challenge is your chance to recreate your favorite retro arcade style game using Amazon Q Developer’s agentic coding experience in the command line interface, Q Developer CLI.

Participate Now

Top comments (0)

Runner H image

Automate Your Workflow in Slack, Gmail, Notion & more

Runner H connects to your favorite tools and handles repetitive tasks for you. Save hours daily. Try it free while it’s in beta.

Try for Free

👋 Kindness is contagious

Explore this insightful write-up, celebrated by our thriving DEV Community. Developers everywhere are invited to contribute and elevate our shared expertise.

A simple "thank you" can brighten someone’s day—leave your appreciation in the comments!

On DEV, knowledge-sharing fuels our progress and strengthens our community ties. Found this useful? A quick thank you to the author makes all the difference.

Okay