Monday, September 8, 2008

The cheapest code is the code that doesn't exist

We have a common idiom in software: the fastest code is the code that doesn't execute. For non-nerds, this basically means that you want to structure your code so that you don't execute unnecessary computations.

As a corollary, I'll propose that every line of code you write (or every line of code you generate through C++ template facilities) is a line that costs you something in maintenance. Thus, the cheapest code is the code that doesn't exist.

It seems obvious, and it should be. The problem (imho) is that programmers are drinking the kool-aid. There's this myth that programming is a really hard, complicated problem. For some classes of programming, it's true. It is hard. Most programming is not hard, though. You think about a problem, you think about how you would sove the problem by hand and you speak another language to solve the problem in a more automatic way.

Unfortunately, it seems that oftentimes programmers overthink problems. They solve problems that don't exist or they try to build scaffolding around an otherwise simple problem. And that results in an unmaintainable blob. This is often done in the name of 'code reuse.' Debunking the notion that code reuse is always a good idea is a subject for another post, though.

Cheers.

1 comment:

Bogatyr said...

On "difficulty": there's a bit more to it. To acquire skill (the ability to do something "without difficulty") in any activity requires theory and practice in combination (along with some amount of inborn talent, and discipline to stay with acquiring the required theory and practice). Usually many years worth. After all, performing a piano conerto is not difficult, you just push the keys according to how the little black dots on the page tell you to.

The kind of structured thinking it takes to break down a problem into steps and translate them into a computer language is a skill like any other -- it takes years of theory and practice to get "good" at it. Some people have brains that just don't work that way. For them, programming is incredibly difficult even with study and practice. For those whose minds tend to work in the way that programming requires, programming is "easy."