On Wed, 04 Mar 2009 21:28:17 +0300, Walter Bright <[email protected]> 
wrote:

Denis Koroskin wrote:
Another question is the feature's usability. It might turn out to be not very handy to have all variables initialized, but we can't know until we really start using it, right?

I've used compilers that required explicit initializers for all variables. Sounds great in theory, in practice it *causes* bugs.

What happens is the compiler dings the user with "initializer required." The user wants to get on with things, so he just throws in an initializer, any initializer, to get the compiler to shut up.

The maintenance programmer then finds himself looking at:

int x = some_random_value;

and wondering why that value, which is never used and makes no sense because the rest of the logic assigns x a proper value before it gets used anyway. So he wastes time figuring out why that dead initializer is used.

Then the next maintenance programmer, with a poor understanding of the code, changes the logic so now x's some_random_value actually gets used, and bug happens.

In general, it's a bad idea to force the user to throw in dead code to shut the compiler up. A particularly illustrative case of this is Java's exception specification, which they loosened up after it became clear that this was not a good idea.

In fact, that's what happen right now. It's just T.init instead of some_random_value, 
which is, in fact, "some random value" that is different for different T (0 for 
ints, 0xFF for char, NaN for floats etc).

I believe C# compiler does the right think. It allows variables to stay 
uninitialized as long as they are not accessed:

int main()
{
   int i; // not an error
   //return i; // error: use of uninitialized variable

   i = 0;
   return i; // fine
}

This effectively elliminates the need for stupid dummy initializers.

Reply via email to