Chapter 16. Premature Optimization

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.

Donald Knuth, “Structured Programming with go to Statements”

16.0 Introduction

Premature optimization is a big problem in the industry. Every developer is fascinated by computational complexity and the beauty of the fastest algorithms. Deciding where and when to make an optimization is one of the differences between a junior developer and a senior one. Optimizations are not free since they add a lot of accidental complexity and you must apply them very carefully. Complex solutions carry your model far away from the real world because they add many obscure layers between the model and the business objects, violating the bijection principle. They also damage readability. You should apply them only under strong factual evidence.

Computational Complexity

Computational complexity studies the resources required to solve computational problems. The most important are time and memory. It measures and compares the efficiency of algorithms and computational systems regarding these resources.

Modern artificial intelligence assistants help you optimize your code. You can become a technological ...

Get Clean Code Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.