View mode: basic / threaded / horizontal-split · Log in · Help
August 20, 2010
Re: Unused variables, better as error or warning?
Walter Bright:

> Not every problem is worth using a sledgehammer to deal with.

Giving a warning where you have not used a variable is not a sledgehammer, it's a light thing, that helps keep code tidy and once in a while helps avoid bugs.

I have not your experience with the troubles caused by warnings, so the last word is of course yours.

But I have another idea, static reflection, similar to the one in the Mozilla Treehydra project. DMD may add a __traits(allVariables, foo) that lists all variables used inside the function foo, and then another __traits(unusedVariable, foo, x) that returns true or false if the variable x inside the function foo is never used :-)

With few similar traits used by a (even in Phobos, if you want) library it's possible to analyse code and implement warnings as desired.

This static reflection, combined with user defined @attributes allows to create simple extensions to the type system, as Treehydra does, but with no need to use another language (as JavaScript, used by the Mozilla project).

Bye,
bearophile
August 20, 2010
Re: Unused variables, better as error or warning?
On 20/08/2010 22:28, Jonathan M Davis wrote:
> On Friday, August 20, 2010 14:12:09 Walter Bright wrote:
>
> Warnings are good for things which you _should_ fix but don't have to immediately

No they aren't. Warnings are good for exactly nothing.

Warnings suck massive arse, hide geniune problems and create useless noise

In the real world where I have to work, warnings are things that other 
useless morons produce and ignore ad infinitium and I have to fix.

It's gotten to the point why I've literaly threatened to hit people with 
a keyboard because they keep checking code into CVS that has warnings in 
it, even though our company coding standards state (in large bold type) 
that this is completely unexceptable.

If it's wrong, it's *always* wrong or it's *not* wrong.

Pick one and learn the rules of the language.

-- 
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk
August 20, 2010
Re: Unused variables, better as error or warning?
bearophile wrote:
> Walter Bright:
>> Not every problem is worth using a sledgehammer to deal with.
> Giving a warning where you have not used a variable is not a sledgehammer,
> it's a light thing, that helps keep code tidy and once in a while helps avoid
> bugs.

My point is it is not a light thing. It negatively and irritatingly impacts 
normal coding practices. Adding in pragmas uglifies the code. More compiler 
switches increases the cognitive load.

(I've seen many build scripts for C/C++ that include a long list of switches. I 
ask the developer what those switches do, he often has no idea. He just copied 
the list of switches from some other project.)

So it's a tradeoff. How much time do you save when it finds an actual bug, vs 
how much time it costs you to futz around trying to suppress invalid warnings.
August 21, 2010
Re: Unused variables, better as error or warning?
Walter Bright, el 20 de agosto a las 14:12 me escribiste:
> bearophile wrote:
> >So if you give me this warning, you are free to keep it always deactivated in
> >all your future D programs :-)
> 
> I detest warnings because they make the language rules "fuzzy". Code
> should be legal or illegal. Wishy-washy warnings make code
> non-portable because different compilers implement different
> warning.

I know this has been discussed to death, but you know, you can ignore
warnings, it doesn't make your code invalid. And having different
options to enable/disable some types of warnings make things even more
manageable. I know you don't like having lots of options in the compiler
either, so I don't expect you to do that either.

Fortunately there are other compilers :)

-- 
Leandro Lucarella (AKA luca)                     http://llucax.com.ar/
----------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------
- Los romanos no tenĂ­an paz, loco... Necesitaban un poco de chala...
August 21, 2010
Re: Unused variables, better as error or warning?
dsimcha, el 20 de agosto a las 13:42 me escribiste:
> == 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.

I never understood this argument against flagging errors/warnings on
unused imports.

lib/a.d:
void f() {}

lib/all.d:
public:
import lib.a;
import lib.b;
import lib.c;

unused.d:
void g() {}

user.d:
import lib.all;
import unused;
f();

import lib.all should never flag an error/warning, because at least one
symbol in lib.all is used. Public imports are treated as if they were
declared in the module that imports them. import unused should flag an
error/warning, since no symbol in unused is used by user.d.

Do you see any problem with that?

-- 
Leandro Lucarella (AKA luca)                     http://llucax.com.ar/
----------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------
It's not a lie, if you believe it.
	-- George Constanza
August 21, 2010
Re: Unused variables, better as error or warning?
Leandro Lucarella:
> Fortunately there are other compilers :)

Regarding this I have appreciated few features of LDC that make it more practical for real usage, as the pragma(allow_inline) and few other practical-oriented things.

Bye,
bearophile
August 21, 2010
Re: Unused variables, better as error or warning?
On 08/20/2010 08:53 PM, Jonathan M Davis 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

I don't get this point of view.

I mean, either you care about them, in which case they may as well be 
errors, or you don't care, in which case you don't need to see them at all.

When you insert a debug-ish early return, do you really want to see the 
warning text every time you compile?

I think what dmd does is great. :-)
Next ›   Last »
1 2 3
Top | Discussion index | About this forum | D home