Article
Dev Principle #10: Do Performance Optimization When Needed
Donald Knuth is famous for saying “Premature optimization is the root of all evil.” There is a lot of good advice packed into that short statement.
The full context of this quote sheds even more light on why it’s important to think about optimization during development:
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%.
Now that we know the danger posed by premature optimization, what impact does this have on the software development process?
- The modern frameworks and languages that are available today go to great lengths to make sure they are running with performance in mind. Generally speaking, there is no need to worry about garbage collection of resources or keeping your memory footprint small after performing complex calculations. Those will be handled (and handled far better) by the frameworks and languages.
- The capacity of the servers that your apps run on have memory and disk space to spare, so there is no need for one letter variables and odd ternary operator tricks.
- Common tasks like database queries and asynchronous operations are now written out in a few lines of code. Your framework takes care of the rest – often with better performance than if you wrote it out from scratch.
By keeping these points in mind, you can focus on writing cleaner code that is easier to maintain. While keeping performance in mind will always be an important aspect of your solution, it should not be the driving force of your project unless absolutely necessary, given the general optimizations and machine power already at your disposal. The code you write may actually be less performant and could make it difficult to maintain.
What about the critical 3% of optimization mentioned above? Setting up profiling and benchmarking tools provides a great way to track where the bottlenecks that are causing performance issues are located in your code. There are plenty of performance analysis tools out there for the language you are working in. Additionally, services like loader.io and New Relic can help stress test your web sites and servers to see where your performance begins to suffer. Finally, you can set up monitoring on your database server to see where queries are performing poorly. Oftentimes these tests are easy to set up and can provide key places in which optimization is possible.
With all of the strides made in programming languages and frameworks, you can focus on writing good, clean, maintainable code. Let the underlying system worry about optimizing itself. You should only dig in and start doing performance optimizations when deemed necessary and after performance tests have shown you where to actually update.