Don't Initialize Variables?

posted on 2005-07-21 at 00:10:13 by Joel Ross

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?

Categories: Development