View mode: basic / threaded / horizontal-split · Log in · Help
August 20, 2010
Re: Unused variables, better as error or warning?
On Aug 21, 10 02:53, Jonathan M Davis wrote:
> On Friday, August 20, 2010 11:35:48 Nick Sabalausky wrote:
[snip]
>>
>> 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

You can treat warnings as warnings with '-wi' instead of '-w'.
August 20, 2010
Re: Unused variables, better as error or warning?
Jonathan M Davis wrote:
> 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.

Having such be errors (or warnings) makes for a very annoying experience. For 
example, when you're commenting out code trying to find a problem, or when 
you're generating D source code from some DSL, etc., having unused variables or 
assignments happen often.
August 20, 2010
Re: Unused variables, better as error or warning?
"KennyTM~" <kennytm@gmail.com> wrote in message 
news:i4mk20$306o$1@digitalmars.com...
> On Aug 21, 10 02:53, Jonathan M Davis wrote:
>> On Friday, August 20, 2010 11:35:48 Nick Sabalausky wrote:
> [snip]
>>>
>>> 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
>
> You can treat warnings as warnings with '-wi' instead of '-w'.

Yea, I bitched and bitched and bitched about that until Walter finally caved 
:)
August 20, 2010
Re: Unused variables, better as error or warning?
On Friday 20 August 2010 12:01:54 Andrej Mitrovic wrote:
> 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

I missed that. Thanks for the info. Every other compiler that I've ever used has 
shown warnings by default (though some of them let you use flags to decide what 
should be flagged as a warning), so I find dmd's behavior is the regard to be 
rather odd. But if we have -wi, then that solves the problem.

- Jonathan M Davis
August 20, 2010
Re: Unused variables, better as error or warning?
On Aug 21, 10 03:35, Walter Bright wrote:
> Jonathan M Davis wrote:
>> 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.
>
> Having such be errors (or warnings) makes for a very annoying
> experience. For example, when you're commenting out code trying to find
> a problem, or when you're generating D source code from some DSL, etc.,
> having unused variables or assignments happen often.

That's why other compilers support fine-grained warning control! :)
August 20, 2010
Re: Unused variables, better as error or warning?
Walter Bright:

>Having such be errors (or warnings) makes for a very annoying experience. For example, when you're commenting out code trying to find a problem, or when you're generating D source code from some DSL, etc., having unused variables or assignments happen often.<

In this answer assume here you are talking about unused variable warnings (UVW). And your conclusion that UVW are bad is wrong.

I have appreciated UVW in all languages where they were presents, and I'm using C for some time. I also know you have a long C experience. (So are you refusing UVW because the DMD back-end makes it hard to implement them? I think of you as an honest person, so I refuse this hypotesys).

UVM are optional, so if you compile code when you have some commented out code, you are free to ignore the warnings (that can't be many if the code is well written) or you are free to deactivate warnings in this specific situation.

In the case of automatic generation of D code, this case is quite less common than normal code where the UVW can help you spot bugs and keep your code clean and tidy. And if this becomes really a problem a pragma can be created to allow you to disable UVW where for simplicity you generate code that may contain unused variables.

So if you give me this warning, you are free to keep it always deactivated in all your future D programs :-)

Bye,
bearophile
August 20, 2010
Re: Unused variables, better as error or warning?
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.
August 20, 2010
Re: Unused variables, better as error or warning?
Walter Bright:
> 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 remember this problem you have explained in past.

In normal C code (not generated code and not while you debug) if you define a variable and then you don't use it then the compiler usually removes it and nothing of value is lost. And not using a variable is accepted by the C standard, so it can't be an error in C.

On the other hand, in a hairy C program of mine I have defined a counter variable, and then I have forgotten to use it, the unused variable warning given by GCC has given me a hint, and I have quickly fixed the code in few seconds.

So such situation is not an error, but it's a code smell (http://en.wikipedia.org/wiki/Code_smell ). Programming is not a binary thing, sometimes what you do is a symptom of a possible hidden problem (or a sign of correct but lazily written code, or correct code that in successive changes may develop a bug). But some times you want to break those conventions, despite they smell. Warnings probably are fuzzy because the programming reality is fuzzy.

If D will become successful then surely people will write a lint able to spot unused variables, it's a basic operation, and it's useful especially if the compiler can't do it.

The alternative is then to do as I think Go has done, and turn unused variables into errors. And then you need a way (like a pragma) to locally disable this error when you are debugging (and this is not nice), or when you generate code automatically (this is acceptable).

Another possible solution is to define uninitialized variables as errors only when D code is compiled in optimized build. This probably solves the problem with such spurious errors while debugging (you probably don't debug your code compiling in optimized mode). I think DMD is already doing something like this for used of uninitialized class instances, that are caught only if you compile code with -O.

Bye,
bearophile
August 20, 2010
Re: Unused variables, better as error or warning?
On Friday, August 20, 2010 14:12:09 Walter Bright wrote:
> 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.

Warnings are good for things which you _should_ fix but don't have to immediately 
while working on code. So, things like unused variables or unreachable code work 
well as warnings. You fix them before you're done, but they don't stop you from 
messing around with your code (or at least don't make you make extra changes 
when messing around with your code). Other than that, I'm inclined to believe 
that they should just all be errors. They all need to be gone when the code is 
finished anyway. But I do think that warnings can be valuable for indicating 
problems that you can temporarily ignore while rearranging and debugging code.

- Jonathan M Davis
August 20, 2010
Re: Unused variables, better as error or warning?
bearophile wrote:
> On the other hand, in a hairy C program of mine I have defined a counter
> variable, and then I have forgotten to use it, the unused variable warning
> given by GCC has given me a hint, and I have quickly fixed the code in few
> seconds.

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


> The alternative is then to do as I think Go has done, and turn unused 
> variables into errors. And then you need a way (like a pragma) to locally 
> disable this error when you are debugging (and this is not nice), or when you
>  generate code automatically (this is acceptable).

I've used pragmas to turn of warnings before. Sorry, but "bleh".
1 2 3
Top | Discussion index | About this forum | D home