Docs: Section on local variables

Famous none at none.net
Sat Apr 21 06:19:10 PDT 2012


Even if my words count for little:

1. If it would be possible to reliably detect reading access to
not explicitly initalized variables the best solution would be to
forbid reading access before initialization and throw an error
message. (Particularly, in this world, there would be no default
initialization.)

2. Unfortunately, it seems as if there are situations where it is
not (actually or virtually) possible to decide whether a variable
is initialized. This means an error message can be thrown only if
the compiler can surely decide whether a variable is initialized.
Otherwise, a warning could be given. In practice, of course, this
could lead to a flood of warning messages (some of which will be 
false positives).

3. In presence of default initialization, it is impossible to
read uninitialized variables. That's it? To me, this sounds a bit 
too easy.

Often, the default value is not what we want. If the default
value is what we want then we have two possibilities:

   * rely on default initialization,
   * do explicit initialization.

If we find a defintion without explicit initialization it is
unclear whether the author intended to rely on default
initialization.
Practically, it seems to be good style, not to rely on default
initialization at all but explicitly initialize all variables.
This requires discipline. Effectively, default initialization
does not help a disciplined programmer at all. On the other hand,
it does not hurt a disciplined programmer. :-P

It would certainly ease life of a developer if warnings about

   (a) surely,
   (b) possibly

not explicitly initialized variables could be emitted optionally. 
For disciplined programmers, errors of type (a) would be even 
more desirable.


More information about the Digitalmars-d mailing list