Jump to page: 1 2 3
Thread overview
Unused variables, better as error or warning?
Aug 20, 2010
bearophile
Aug 20, 2010
dsimcha
Aug 20, 2010
Simen kjaeraas
Aug 21, 2010
Leandro Lucarella
Aug 20, 2010
Pedro Rodrigues
Aug 20, 2010
Andrej Mitrovic
Aug 20, 2010
bearophile
Aug 20, 2010
Nick Sabalausky
Aug 20, 2010
Jonathan M Davis
Aug 20, 2010
KennyTM~
Aug 20, 2010
Nick Sabalausky
Aug 21, 2010
Pelle
Aug 20, 2010
Andrej Mitrovic
Aug 20, 2010
Jonathan M Davis
Aug 20, 2010
Walter Bright
Aug 20, 2010
KennyTM~
Aug 20, 2010
bearophile
Aug 20, 2010
Walter Bright
Aug 20, 2010
bearophile
Aug 20, 2010
Walter Bright
Aug 20, 2010
bearophile
Aug 20, 2010
Walter Bright
Aug 20, 2010
Jonathan M Davis
Aug 20, 2010
div0
Aug 21, 2010
Leandro Lucarella
Aug 21, 2010
bearophile
Aug 20, 2010
Jonathan M Davis
August 20, 2010
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
August 20, 2010
== Quote from bearophile (bearophileHUGS@lycos.com)'s article
> 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

If we make unused imports an error, how is anyone supposed to do import someSmallLibrary.all?  If you make unused imports an error, the collective cost of extra import declaration boilerplate will probably be larger than the GDP of some African countries.
August 20, 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@lycos.com> wrote in message news:i4luk9$2rdg$1@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 

August 20, 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@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
>
August 20, 2010
Something I was forgetting, I have an open enhancement request about this, by the way: http://d.puremagic.com/issues/show_bug.cgi?id=3960

Bye,
bearophile
August 20, 2010
dsimcha <dsimcha@yahoo.com> wrote:

> If we make unused imports an error, how is anyone supposed to do import
> someSmallLibrary.all?  If you make unused imports an error, the collective cost of
> extra import declaration boilerplate will probably be larger than the GDP of some
> African countries.

Only give warnings for this for modules specified on the command-line.
////
module foo;
import bar;
////
module bar;
import unused;
////

dmd foo
  No problem (as long as the compiler magically finds bar)
dmd foo bar
  Warning!


-- 
Simen
August 20, 2010
"bearophile" <bearophileHUGS@lycos.com> wrote in message news:i4luk9$2rdg$1@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.
>

An error would be an enormous pain in the ass. A warning might be helpful in some cases.


August 20, 2010
On Friday, August 20, 2010 11:35:48 Nick Sabalausky wrote:
> "bearophile" <bearophileHUGS@lycos.com> wrote in message news:i4luk9$2rdg$1@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.
> 
> An error would be an enormous pain in the ass. A warning might be helpful in some cases.

Except that thanks to how warnings are deal with in dmd, there's not much difference between an error and a warning; it's just a question of how picky you want to be about errors. As it is, I'd argue that there is no such thing as a real warning in D. You never see warnings unless you use -w, at which point they're treated as errors. And if you're being at all careful, you're going to be compiling with -w, so it doesn't make much difference. You can choose to compile without -w until you think what you have works and then use -w to find stuff you missed, but then you could easily be being shot in the foot by something that's considered a warning. If you had seen it, you could have dealt with it. What dmd needs is for warnings to be visible in normal compilation, making -w only make warnings errors as opposed to being the way to make them appear as well. As it is, warnings pretty much might as well be errors.

- Jonathan M Davis
August 20, 2010
Yes there are, you enable them with -wi, which are informational warnings. Some errors are listed here, but I'm not sure which of these can be used with -wi:

http://www.digitalmars.com/d/2.0/warnings.html



On Fri, Aug 20, 2010 at 8:53 PM, Jonathan M Davis <jmdavisprog@gmail.com> wrote:
> On Friday, August 20, 2010 11:35:48 Nick Sabalausky wrote:
>> "bearophile" <bearophileHUGS@lycos.com> wrote in message news:i4luk9$2rdg$1@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.
>>
>> An error would be an enormous pain in the ass. A warning might be helpful in some cases.
>
> Except that thanks to how warnings are deal with in dmd, there's not much difference between an error and a warning; it's just a question of how picky you want to be about errors. As it is, I'd argue that there is no such thing as a real warning in D. You never see warnings unless you use -w, at which point they're treated as errors. And if you're being at all careful, you're going to be compiling with -w, so it doesn't make much difference. You can choose to compile without -w until you think what you have works and then use -w to find stuff you missed, but then you could easily be being shot in the foot by something that's considered a warning. If you had seen it, you could have dealt with it. What dmd needs is for warnings to be visible in normal compilation, making -w only make warnings errors as opposed to being the way to make them appear as well. As it is, warnings pretty much might as well be errors.
>
> - Jonathan M Davis
>
August 20, 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
« First   ‹ Prev
1 2 3