1

I was reading on the net (http://www.codinghorror.com/blog/2005/07/for-best-results-dont-initialize-variables.html) that we should not initialize variables.

Somehow I dont get it. Often I just cannot avoid that. Lets look on a simple example:

public int test(string s)
{
  int start = 0;
  int mod = 2;
  int output = 0;

  foreach (int i in s)
  {
    output = output + (i % mod) + start;
    start++;
  }

  return output;
}

Ok its maybe a nonsense :-) But the question is: can I avoid the initialization? Maybe its not possible for mod, because mod have to be 2 from the beginning and it will stay 2. But how about start and output? I just cannot write int start because thats always Error Use of unassigned local variable. Maybe int start = null would be better, but in this case its not gonna work too. So how to avoid this stuff?

miri
  • 1,611
  • 5
  • 20
  • 24
  • 1
    foreach (int i in s) probably won't compile? – Anton Jul 12 '12 at 13:47
  • 4
    Don't bother. That article boils down to extreme micro-optimization, and the proposal has near-zero effect. You'd have to write even more code to check for an uninitialized `output` and assign an value if true. For value types, it's a 100% non-issue. – p.campbell Jul 12 '12 at 13:48
  • 1
    @Anton it will :) as you can see here http://msdn.microsoft.com/en-us/library/system.string.aspx it implements IEnumerable – albertjan Jul 12 '12 at 13:49
  • @the_ajp if ints are 0 by default, why is visual studio forcing me to initialize it even if I want 0? As I said if I dont do that I get Error Use of unassigned local variable – miri Jul 12 '12 at 13:51
  • @Anton, yes it will. It will implicitly cast char to int – magritte Jul 12 '12 at 13:52
  • @Anton: foreach will enumerate and (try to) typecast. This one should work. – H H Jul 12 '12 at 13:52
  • Even though int default to zero, variables that will be used outside of the scope still need to be initialised before use/returning – stevethethread Jul 12 '12 at 13:55
  • @the-ajp Uh, you're right. That's bad. I mean the thing it will compile and run. Strong typed environment. – Anton Jul 12 '12 at 13:56

4 Answers4

5

You've misread his article. In his article he is specifically talking about initialization of variables with respect to classes. In the case you've put forth, your variables should be initialized before they can be used because they'll be immediately used.

Edit: Yes, in this specific case the int variables don't need initialization because the compiler automatically initializes an int to 0, but if this is taken to a different degree with a string or a DateTime, initialization becomes important in the context of a method.

Joel Etherton
  • 37,325
  • 10
  • 89
  • 104
  • In this case, only the returned variable needs to be initialised, before being returned from the method. – stevethethread Jul 12 '12 at 13:54
  • 1
    @SteveSolomon: Micro-analyzing his specific example ignores the point of his original question. Replace the ints with strings or DateTimes and initialization becomes immediately relevant. – Joel Etherton Jul 12 '12 at 13:55
  • @HenkHolterman: In classes, yes, but in methods I've received compile time errors when not explicitly initializing a DateTime before using it. – Joel Etherton Jul 12 '12 at 17:25
5

You misread the article. The article is talking about member variables (which are automatically initialized to default values and therefore do not require explicit initialization), but you are trying to apply the rule to local variables (which are not automatically initialized and therefore require explicit initialization).

Raymond Chen
  • 44,448
  • 11
  • 96
  • 135
1

You can rewrite you method like this

public int Test(string s) {
            const int mod = 2;
int start;                
int output = 0;

            foreach(int i in s) {
                output = output + (i % mod) + start;
                start++;
            }

            return output;
        }

In this case, the start variable does not need to be initialised, and that is true whether declare in inner or outer scope.

However, the output variable does need initialisation due to the fact that it will be returned by the method, and is possible that if the loop never runs, the variable would never be initialised.

stevethethread
  • 2,524
  • 2
  • 30
  • 29
-2

The article is talking about not to initialize variables with default values. For example,

int x = 0;

is not good. Also, you should initialize (and declare) the variable just before it's usage is a clean code.

Initialization in constructor is not just before the usage.

Anton
  • 1,409
  • 1
  • 19
  • 37