Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What optimizations are OK to do right away?

One of the most common mantras in computer science and programming is to never optimize prematurely, meaning that you should not optimize anything until a problem has been identified, since code readability/maintainability is likely to suffer.

However, sometimes you might know that a particular way of doing things will perform poorly. When is it OK to optimize before identifying a problem? What sorts of optimizations are allowable right from the beginning?

For example, using as few DB connections as possible, and paying close attention to that while developing, rather than using a new connection as needed and worrying about the performance cost later

like image 701
cdeszaq Avatar asked Mar 31 '09 20:03

cdeszaq


People also ask

Can you not optimize early?

Trying to do the optimization too early can be a futile time-waster. Every programmer with a few years' experience or education has heard the phrase "premature optimization is the root of all evil." This famous quote by Sir Tony Hoare (popularized by Donald Knuth) has become a best practice among software engineers.

When should I optimize?

When to optimize. Optimization can reduce readability and add code that is used only to improve the performance. This may complicate programs or systems, making them harder to maintain and debug. As a result, optimization or performance tuning is often performed at the end of the development stage.

What is premature optimization?

Premature optimization involves trying to improve something—especially with the goal of perfecting it—when it's too early to do so, for example by spending a lot of time perfecting a certain piece of software code, even though it's unclear whether this code will actually be needed.


1 Answers

I think you are missing the point of that dictum. There's nothing wrong with doing something the most efficient way possible right from the start, provided it's also clear, straight forward, etc.

The point is that you should not tie yourself (and worse, your code) in knots trying to solve problems that may not even exist. Save that level of extreme optimizations, which are often costly in terms of development, maintenance, technical debt, bug breeding grounds, portability, etc. for cases where you really need it.

like image 112
MarkusQ Avatar answered Oct 08 '22 23:10

MarkusQ