Local variables should always be initialized, right?

Again and again I see programmers doing the following:

    string emailAddress = string.Empty; 

    if( /*condition*/ ) 
        //do some stuff 
        emailAddress = retrieve_email_address(); 
        //use emailAddress 

You may see it with setting strings to String.Empty as in the example, or you may see it with setting pointers to null, integers to zero, etc. There are two problems here; the first one is easy to see, the second one might not be so easy.

First of all, emailAddress was declared outside of the scope in which it is being used. That's not a very good idea; it should have been declared within the scope in which it is being used, so the code should instead read string emailAddress = retrieve_email_address(); That one was simple. But if we ignore it for a moment, there is another, actually worse problem with the above code.

Some people believe that when declaring a local variable you should always initialize it with some initial value, and they follow this rule with superstitious devotion. This is dead wrong. It may have been advisable back in the dark ages of the first C compilers, but not anymore.

Modern compilers of C, C++, C# and Java are quite good at warning you if a variable might be read before it has been initialized. So, by pre-initializing it at declaration time with a value which is by definition meaningless, (since a meaningful value is not yet known at that time,) you are circumventing the safety checks of your compiler, and you are opening up the possibility of error: if you forget to assign a meaningful value to your variable further down before you make use of it, the compiler will not warn you, because as far as the compiler knows, the variable has already been initialized at declaration time.

(That was from an answer of mine on CodeReview.StackExchange.com.)

Furthermore, when a programmer sees a value being initialized to a certain value, they are inevitably tempted to think that this value has a certain role to play in the algorithm which follows. For example, setting an integer to zero makes one think that what follows must be a loop using that integer as an accumulating sum. So, when that's what you expect, and you read further down only to find that the variable is re-initialized with a different value, it is rather disappointing.


1 comment:

  1. Your point would be 200% right if only compilers always worked right. For example, when you have an if statement checking AAA Is Nothing, VS warns you that AAA might be Nothing during this call, so be careful... I guess I have to thank Microsoft for that, but the point stands. :)