Unused variables, better as error or warning?

Andrej Mitrovic andrej.mitrovich at gmail.com
Fri Aug 20 08:44:30 PDT 2010


I think I've read somewhere (either the spec or TDPL) that states
unused variables are errors. But I don't agree with that. Make it a
compiler flag if it's really needed. A warning is ok for me.

If I'm just trying out some code, I might use one or another variable
while declaring both. If I only use one and it's an error to leave a
variable unused, then I'm forced to always comment out variables which
I'm not using. It would be too painful to enforce such restrictions
while trying out new code.

On Fri, Aug 20, 2010 at 3:06 PM, bearophile <bearophileHUGS at lycos.com> 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
>


More information about the Digitalmars-d mailing list