Null references (oh no, not again!)

Ary Borenszweig ary at esperanto.org.ar
Wed Mar 4 11:18:06 PST 2009


Walter Bright 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.

It's not like that. They don't require you to initialize a variable in 
it's initializer, but just before you read it for the fist time. That's 
very different.

If you force the user to do that, you are never wrong: if the user 
didn't initialize the variable before reading it's value, then it's an 
error; if the variable is never read, then it's dead code and the 
compiler can point that out. What's wrong with that? You always assign a 
value to a variable *a first time*!

See also Denis Koroskin's answer.



More information about the Digitalmars-d mailing list