posts - 103, comments - 4, trackbacks - 37

My Links

News

Tag Cloud

Archives

Don't Initialize Variables?

Jeff Atwood at Coding Horror is suggesting that we shouldn't be initializing our variables, and then he provides some performance numbers. What he doesn't mention, and Scott from Lazy Coder calls him on in the comments, is what a 35% performance hit really means.

Maybe I'm wrong, but I always thought you shouldn't rely on the compiler to do your initializations. If you do, and the compiler ever changes (or you used a different compiler that doesn't initialize the same), you may get unexpected results. For example, if the compiler team decided ints should be initialized to -1 instead of 0, and you rely on it being initialized to 0, then your code breaks for no apparent reason.

I just did a quick search, and the definitive development book, Code Complete, offers a checklist that includes initializing variables as one of the items to check. In fact, whenever the book talks about performance tuning, it says only to worry about that after the fact and you've got solid evidence of where your bottleneck is.

So while initializing variables may be quicker, what's the likelihood that removing variable initializations will boost your performance enough to make the difference?

Print | posted on Wednesday, July 20, 2005 4:01 PM |

Feedback

Gravatar

# re: Don't Initialize Variables?

I read this article, and had the exact same sentiments as you do. I feel that all you gain by doing this is causing a code maintenace nightmare, but in saying that, the topic of code maintenace is a subjective one, which is often left up to the developers personal taste, and often the "right thing to do" is forgotten. :)
7/21/2005 9:37 AM | Kevin
Gravatar

# re: Don't Initialize Variables?

You're right about the personal taste thing. The problem is that it's not usually the developer who wrote the code who has to maintain it. As a consultant, I know that to be the case - I rarely maintain my own code! It's always passed on to the client to handle.
7/21/2005 10:57 AM | Joel Ross
Gravatar

# re: Don't Initialize Variables?

> I feel that all you gain by doing this is causing a code maintenace nightmare

I fail to see how less code is a "maintenace nightmare".

It's unnecessary code. The fact that it performs better is just an added bonus, not the rationale.
7/22/2005 1:33 AM | Jeff Atwood
Gravatar

# re: Don't Initialize Variables?

It's not unnecessary code though. It's code that is written for you by the compiler, and if something (like the compiler) ever changes, you won't be in control any more. That's a maintenance nightmare.

And less code doesn't mean easier maintenance either. There are plenty of examples of programmers being "slick" by reducing the amount of code, but in reality, they are increasing complexity, which makes maintaining the code more difficult.
7/22/2005 1:42 AM | Joel Ross
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 

Powered by: