Docs: Section on local variables

Jakob Ovrum jakobovrum at gmail.com
Fri Apr 20 18:53:08 PDT 2012


On Saturday, 21 April 2012 at 01:31:45 UTC, Jonathan M Davis 
wrote:
> Okay. I should have said that Java and C# require that a 
> variable be written
> to before being used. Regardless, they take the approach of 
> requiring that a
> variable be initialized/assigned a value rather than letting it 
> be garbage
> (like C/C++) or initializing it to a default value (like D).

It solves only part of the problem. If a simple, unconditional 
default-initialization was an objectively superior solution, 
nobody would bother implementing control flow graphs.

When "int x;" and "int x = 0;" are semantically identical, the 
intention of the programmer isn't clear in the case of "int x;" 
(talking about local variables only, of course). Is the default 
value relied upon intentionally in the program? What if it 
unintentionally reads the default value? The default value is 
more helpful than garbage when debugging (especially for 
non-integer types), but with a proper implementation, errors like 
these can be caught at compile-time.

You could introduce coding conventions in your project to manage 
the issue, but then we're in faith-based programming territory.

>
>> The control flow graph takes care of correctly determining
>> whether a variable has been initialized.
>
> And Walter seems to hate flow analysis like the plague (due to 
> its increased
> complexity IIRC).

It's well-trodden area, it doesn't have to be difficult.

>_Very_ little in D requires or does flow
> analysis.

And D is worse off because of it.


More information about the Digitalmars-d mailing list