The Root of All Evil

A very wise programmer told me “premature optimization is the root of all evil.” (Since publishing this, I’ve learned that my wise friend was quoting Knuth.  See the comments and Computer Programming as an Art.  Knuth may have been quoting Hoare.)

I think he is on solid theological ground, since you could make a strong case that sin is a result of calculating reward prematurely, without considering longer term consequences.  He meant this, however, as a rhetorical overstatement about a very real and concrete coding mistake.

Many programmers write complicated optimizations because they “know” that the naive approach would be grossly inefficient.  Perhaps they think that by writing a general, parameterized solution, they can avoid future occurrences of duplicated code.   Or maybe when they were in college they had their hands slapped as for hard-coding strings or other values.

The end result is usually unnecessary indirection, with no benefit from the loss of clarity.

Make no mistake: sometimes a naive or brute force approach just won’t scale.  In these cases, experienced programmers will not wait to implement steps that add complexity but are required.  That decision, however, should be based on thoughtful reflection rather than automatic.

When coding, pursuing the simplest approach that could work and the principle of YAGNI should always be the guiding stars.

Ideally, your code is so straight-forward and simple that readers will be tempted to say “What’s the big deal? Anyone could write this.”

That may be true, but somehow newbies never do.


2 thoughts on “The Root of All Evil

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s