-1

Almost every blog I read says compiler flagging error "use of unassigned local variable" says its design decision because history says that unassigned variable are source of bug and compiler wants to stop you doing that.

I am not sure how assigning to a local variable can help me not injecting a bug or even mitigate the chances of introducing bug.

I could have assigned a local variable as a return value of some method that can have bug and eventually result in wrong assignment. So I don't think compiler is helping me in that sense anyway. Had been designers decided to consider default value for unassigned local variables, developers would have taken care of it and in context of "possibly introducing bug" situation would have been same, no ?

Thanks!!

rahulaga-msft
  • 3,964
  • 6
  • 26
  • 44
  • This question is too broad. For most people, it's obvious why [GIGO](https://en.wikipedia.org/wiki/Garbage_in,_garbage_out) applies and should be avoided. For people to whom it's not obvious, it would take a lot more space than is reasonable for a Stack Overflow answer to walk someone through the problem. – Peter Duniho Jan 20 '18 at 20:09
  • An unassigned variable could mean a spelling error in its definition. If an undefined variable is used in code but spelled correctly, the error would be, "undefined variable." But the programmer would think they has defined it. They haven't because they misspelled it in its definition. Adding an unassigned variable warning would highlight the line with the misspelling. – shawnhcorey Jan 22 '18 at 14:03

2 Answers2

2

The use of an unassigned variable can be a potential bug or not (it almost always is). The question here is; if it is a bug, when would you like to know about it? At compile time or at runtime where it can be relatively hard to spot if the code is sufficiently complex or the introduced bug is subtle?

A simple example:

string Join(params object[] array)
{
    string foo;

    foreach (var o in arr)
        foo += o.ToString();
}

So now, we have two options: the compiler lets you compile this happily and watches you crash and burn in runtime, or it tells you straightaway you have bug. The latter choice seems better to me.

Now, when you wrote this question, you were probably thinking in value types more than in reference types; an unassigned reference type is practically an immediate NullReferenceException and, although vexing, easy to fix once you run the code for the first time.

Value types on the contrary have usable default values, but that makes the situation a whole lot worse. Now maybe you forgot to initialize a variable to a sensible value and nothing apparently obvious is going wrong in runtime but maybe you've just introduced a not so obvious bug in your program:

int totalDays(DateTime date)
{
    DateTime now; //TODO: initialize to server time, not local DateTime.Now
    return (now - date).TotalDays;
}

Writing this code you spill your coffee, then you get a phone call from you wife telling you your dog got sick and puked on your favorite (and expensive) rug and again on the way to the vet in your brand new car... by the time you get back you've forgotten about totalDays (I finished writing it didn't I?) and you start working on other stuff in your project. By the end of the day/week, you need to meet a deadline and you ship a bug in your code. The compiler can warn you easily that something smelly is going on. Why on earth wouldn't you want it to?

Convinced?

InBetween
  • 32,319
  • 3
  • 50
  • 90
  • Convinced, because it helps when you forget to assign variable to some value - but not when you forget to assign default value. So yes, for situation like "then you get a phone call...". – rahulaga-msft Jan 21 '18 at 04:11
  • The outcome of program execution when developer **explicitly** assigns local variable to default value or **forget** to assign any value itself is same. Possibility of even happening this itself is alarming. – rahulaga-msft Dec 22 '18 at 15:10
  • @RahulAgarwal The outcome is not the same: one is an error the compiler will tell you about the other isn’t. That is the whole point of the feature. If you explicitly set a variable to its default value then the compiler will understand you know what you are doing as with the rest of your legal code; if it compiles and then blows up a nuclear reactor because it had a bug that’s your problem. The compiler helps you out as much as it can but it’s not it’s job making sure your code is bug free. – InBetween Dec 22 '18 at 15:15
  • I agree. Actually - I should have been more explicit in my comment by saying - "_compiler takes care of avoiding this alarming situation_" :) – rahulaga-msft Dec 22 '18 at 15:25
0

Imagine that you have a variable that is used to multiply another variable, if the variable has no value assigned when the multiplication happens, there'll be an error. That's what the compiler wants to avoid, since the compiler doesn't know wheter the variable will be initialized later or not.

Alex Coronas
  • 468
  • 1
  • 6
  • 21