Unused variables, better as error or warning?

Pedro Rodrigues pdfrodrigues at gmail.com
Fri Aug 20 07:05:45 PDT 2010


I agree, unused variables should be treated as warnings.

In my opinion, warnings should be employed only in situations where the 
compiler detects that the programmer might have made a mistake, but which 
are not impeditive of compiling and running the program. Having unused 
variables clearly is not impeditve of your program compiling and running 
correctly, so if you are going to treat them as errors instead of warning, 
might as well treat all warnings as errors. A good programmer will make sure 
the code compiles without warnings anyway, so claiming that warnings are 
ignored by the programmer is not a valid rationale in my opinion.
And there are situations where you happen to have unused variables and still 
want to be able to compile, as when debugging for example.  It should at 
least be optional whether the compiler treats these situations as errors.

Cheers,

"bearophile" <bearophileHUGS at lycos.com> wrote in message 
news:i4luk9$2rdg$1 at digitalmars.com...
> 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 



More information about the Digitalmars-d mailing list