Docs: Section on local variables

H. S. Teoh hsteoh at quickfur.ath.cx
Sat Apr 21 11:24:51 PDT 2012


On Sat, Apr 21, 2012 at 06:42:06PM +0200, bearophile wrote:
> H. S. Teoh:
> 
> >>Why do you want to be able to declare local variables and then
> >>never use them?
> >[...]
> >
> >It happens a lot when you're debugging code (temporarily comment out
> >some stuff for testing purposes). It would be a major pain if every
> >single time you need to temporarily suppress a section of code, you
> >also have to hunt down every last stray variable that's now no longer
> >referenced in the function and comment them out as well. And then do
> >the reverse after you're done testing whatever it is you're trying to
> >debug.
[...]
> So are you saying that in 'finished' code (when you aren't working on
> it) you don't want unused variables?

In finished code, it's obviously a bad thing to have unused variables
(unless the compiler optimizes them away, but that's not happening 'cos
it depends on flow analysis, which would have let us spit out warnings
about it in the first place.)


> So do you prefer just an unused variable warning that comes out only
> when you use "-wi/-w"? A problem I've seen in D.learn is that lot of
> people here doesn't seem to use -wi/-w. Or maybe, on the contrary,
> this unused variable error should be suppressed only if the D code is
> compiled with "-debug"?

I don't know if conflating unused variable warnings with -debug is a
good thing. Just like the conflation of -release with the opposite of
-debug or -unittest.


> >(Yes, yes, I know variables are supposed to be declared right before
> >they're used, not at the top of the function... but sometimes things
> >move apart after enough code iterations.)
> 
> Then adding some pressure to remind to keep such distance short may be
> a good thing :-)

This is not always possible. Sometimes you *need* to declare a variable
outside a loop, which is only used much deeper inside the loop, because
it needs to persist across iterations. Moving it close to where it's
used breaks its semantics.


> And beside unused variables, there is also this:
> http://d.puremagic.com/issues/show_bug.cgi?id=4694
[...]

In my mind, that's a more general issue that includes detecting unused
variables as a special case. Both depend on flow analysis, which
apparently Walter is not fond of (I can't tell why). Both are nice to
have, but I don't know if it will happen.


T

-- 
Without outlines, life would be pointless.


More information about the Digitalmars-d-learn mailing list