Unused variables, better as error or warning?

Jonathan M Davis jmdavisprog at gmail.com
Fri Aug 20 12:03:29 PDT 2010


On Friday, August 20, 2010 06:06:17 bearophile wrote:
> A small Reddit thread regarding if unused variables and imports are better
> as errors or warnings: http://www.reddit.com/r/programming/comments/d3emo
> 
> In my opinion in this case errors are too much, warning are enough.
> 
> Few situations for those warnings:
> - warning for unused variables (as GC, C# and other compilers do);
> - warning when a variable get used in some ways, and then its last
> assignment gets unused (done by a kind of C compiler); - unused imports
> (useful to keep code clean and remove unnecessary module dependences); -
> unused functions (but this is harder to do in a clean way in a language
> that has templates, so this may be omitted).
> 
> Among those four warnings the most useful are the first two ones. In C once
> the unused variable warning of GCC has found at compile time a bug in my
> code (I did forget to increment that variable in the loop). So I have
> loved this warning ever since.
> 
> Bye,
> bearophile

I agree with #1, #3, and #4 but not #2. It requires control flow analysis to get 
that to work, and Walter generally avoids that. _Maybe_ it would be reasonable 
if it did not include default initialization as an unused assignment (like the 
example in your bug report does), but it's very easy to get into situations 
where you _want_ to assign to the variable well after it's assigned, much as 
it's generally good practice to initialize it yourself at the point of 
declaration. A prime example is if you need to initialize the variable in an 
innner scope, but have to have it exist in the outer scope. It would be stupid 
if you were forced to initialize the variable to make the warning go away in 
this case. It's defaualt initialized.

In more complex cases, the compiler is not going to be able to accurately detect 
when something is or isn't going to be assigned to in ever code path, so you'd 
be forced to initialize it in spite of the fact that it's default initialized. 
This happens all the time in languages like Java, and it's annoying. Default 
initialization takes care of the problem.

So, in all but the simple cases, the compiler is not going to be smart enough to 
reasonably determine whether you're assigning to a variable multiple times 
before it's used. And in the simpler cases where it can, it can optimize away 
the extra assignments. I'm sure that there are cases where it would be nice for 
the compiler to point out that you're uselessly assigning to a variable 
(especially if you're making a useless function call too), but it would cost the 
compiler too much in complexity and cost the programmer in many other cases by 
forcing them to either ignore warnings (which they should pretty much never do) 
or throw in pointless explicit initializations rather than letting the defaults 
do their job.

- Jonathan M Davis


More information about the Digitalmars-d mailing list