www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Unused variables, better as error or warning?

reply bearophile <bearophileHUGS lycos.com> writes:
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 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
Aug 20 2010
next sibling parent reply dsimcha <dsimcha yahoo.com> writes:
== 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 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.
Aug 20 2010
next sibling parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
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
Aug 20 2010
prev sibling parent Leandro Lucarella <luca llucax.com.ar> writes:
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 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
Aug 20 2010
prev sibling next sibling parent "Pedro Rodrigues" <pdfrodrigues gmail.com> writes:
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 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 
Aug 20 2010
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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 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
Aug 20 2010
prev sibling next sibling parent bearophile <bearophileHUGS lycos.com> writes:
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
Aug 20 2010
prev sibling next sibling parent reply "Nick Sabalausky" <a a.a> writes:
"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 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.
Aug 20 2010
next sibling parent reply Jonathan M Davis <jmdavisprog gmail.com> writes:
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 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
Aug 20 2010
next sibling parent reply KennyTM~ <kennytm gmail.com> writes:
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'.
Aug 20 2010
parent "Nick Sabalausky" <a a.a> writes:
"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 :)
Aug 20 2010
prev sibling parent Pelle <pelle.mansson gmail.com> writes:
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 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. :-)
Aug 21 2010
prev sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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 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
Aug 20 2010
prev sibling next sibling parent reply Jonathan M Davis <jmdavisprog gmail.com> writes:
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 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
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
Aug 20 2010
parent reply Walter Bright <newshound2 digitalmars.com> writes:
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.
Aug 20 2010
next sibling parent KennyTM~ <kennytm gmail.com> writes:
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! :)
Aug 20 2010
prev sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
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
Aug 20 2010
parent reply Walter Bright <newshound2 digitalmars.com> writes:
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.
Aug 20 2010
next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
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
Aug 20 2010
parent reply Walter Bright <newshound2 digitalmars.com> writes:
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".
Aug 20 2010
parent reply bearophile <bearophileHUGS lycos.com> writes:
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
Aug 20 2010
parent Walter Bright <newshound2 digitalmars.com> writes:
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.
Aug 20 2010
prev sibling next sibling parent reply Jonathan M Davis <jmdavisprog gmail.com> writes:
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
Aug 20 2010
parent div0 <div0 sourceforge.net> writes:
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
Aug 20 2010
prev sibling parent reply Leandro Lucarella <luca llucax.com.ar> writes:
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...
Aug 20 2010
parent bearophile <bearophileHUGS lycos.com> writes:
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
Aug 20 2010
prev sibling next sibling parent Jonathan M Davis <jmdavisprog gmail.com> writes:
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
Aug 20 2010
prev sibling next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Jonathan M Davis:

I have seen you have commented in the enhancement request too, thank you for
your answers.



get that to work, and Walter generally avoids that.<
The situation here is different. Walter was opposed to forcing initialization of variables and then using flow analysis to spot all cases where the program isn't doing this. Walter has said that some cases can't be determined (and probably also to keep compler simpler But what I am discussing there is not a change in the language, it's a diagnostic thing. A conformant D compiler is free to give or not give warning for unused variables (or variables unused after the last assignment. This is a different warning). This means that it's OK in this case for the compiler to not perform an accurate analysis, because even if it misses some cases it's OK still. So it's a very different situation.
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.<
I think we are talking about two different things. The "unused last assignment" warning means that you assign something to a variable, and then you do nothing with it. So you have wasted efforts, and that is code smell of sloppy or code where you have forgotten to use the variable. This has nothing to do with uninitialized variables, it's kind of the opposite problem. Sorry for lumping two different warnings into the same enhancement request. Bye, bearophile
Aug 20 2010
parent Jonathan M Davis <jmdavisprog gmail.com> writes:
On Friday, August 20, 2010 14:06:49 bearophile wrote:
 Jonathan M Davis:
 
 I have seen you have commented in the enhancement request too, thank you
 for your answers.
 

to get that to work, and Walter generally avoids that.<
The situation here is different. Walter was opposed to forcing initialization of variables and then using flow analysis to spot all cases where the program isn't doing this. Walter has said that some cases can't be determined (and probably also to keep does). But what I am discussing there is not a change in the language, it's a diagnostic thing. A conformant D compiler is free to give or not give warning for unused variables (or variables unused after the last assignment. This is a different warning). This means that it's OK in this case for the compiler to not perform an accurate analysis, because even if it misses some cases it's OK still. So it's a very different situation.
The compile must be just as accurate when dealing with warnings as when dealing with errors. The reason is that any good project is not going to have _any_ warnings in it when it's done. And forcing the programmer to do extra, pointless various places with variable initialization because they do code flow analysis for variable rather than default initialize variables. Because D default initialiazes everething, that sort of code flow analysis becomes unnecessary. For something to be a warning, it has to something that must reasonably be fixed in a program before it's done but can be temporarily ignored. Having the compiler do code flow analysis (which it _can't_ do perfectly due to a lack of information) to determine something is going to result in cases where the program is forced to do something unnecessary just to get rid of the warning. So, in order for such a warning to be put in the language, it has to be worth that extra cost.
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.<
I think we are talking about two different things. The "unused last assignment" warning means that you assign something to a variable, and then you do nothing with it. So you have wasted efforts, and that is code smell of sloppy or code where you have forgotten to use the variable. This has nothing to do with uninitialized variables, it's kind of the opposite problem. Sorry for lumping two different warnings into the same enhancement request.
Your example for this exact problem involved a default-initialized variable rather than double-assignment, so if you didn't intend to lump default- initialized variables in with double-assigned variables, it was a poor example. Regardless, the same type of code flow analysis applies. So, for it to be worth making a warning, it has to be worth whatever pain comes with the compiler getting it wrong. And in this case, that means that either you'd have to _not_ assign to something when you're supposed to (which would obviousy be very bad), or have the compiler not complain when it couldn't be 100% certain. If an algorithm could be devised which was 100% certain and the warning were only given in such cases, then it would likely be acceptable. However, since Walter has generally tried to avoid all such code flow analysis (IIRC mainly because it complicates the compiler and makes it more likely that it's wrong), I wouldn't expect it to ever make it into the compiler. - Jonathan M Davis
Aug 20 2010
prev sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Jonathan M Davis:

 The compile must be just as accurate when dealing with warnings as when
dealing 
 with errors.
allowed, the compiler has to catch them all and report them as errors. If your language (C, D, etc) allows you to not use a variable the last time you have initialized it, and then you want to add a warning that finds such situations, you don't need the compiler to be 100% accurate, even if some of such situations are not detected then it's perfectly OK.
 The reason is that any good project is not going to have _any_ 
 warnings in it when it's done. And forcing the programmer to do extra,
pointless 

 various places with variable initialization because they do code flow analysis 
 for variable rather than default initialize variables. Because D default 
 initialiazes everething, that sort of code flow analysis becomes unnecessary.
Note that in my last posts I have never asked for uninitialized variables warnings or errors, so I think you have misread my posts (I have asked for that months ago).
 Your example for this exact problem involved a default-initialized variable 
 rather than double-assignment, so if you didn't intend to lump default-
 initialized variables in with double-assigned variables, it was a poor example.
That example was not about default-initialized variables nor double-assigned ones, it was about a variable that is not used after the last time it is assigned. (I agree that was not a good example.)
 or have the compiler not complain when it couldn't be 100% certain. If an 
 algorithm could be devised which was 100% certain and the warning were only 
 given in such cases, then it would likely be acceptable.
In the meantime I have seen you have already commented on the bug report and I agree with with you write there. Thank you. For the In this specific case I think "zero false positives" is doable :-) Bye, bearophile
Aug 20 2010
next sibling parent Jonathan M Davis <jmdavisprog gmail.com> writes:
On Friday, August 20, 2010 15:11:35 bearophile wrote:
 Jonathan M Davis:
 The compile must be just as accurate when dealing with warnings as when
 dealing with errors.
not allowed, the compiler has to catch them all and report them as errors. If your language (C, D, etc) allows you to not use a variable the last time you have initialized it, and then you want to add a warning that finds such situations, you don't need the compiler to be 100% accurate, even if some of such situations are not detected then it's perfectly OK.
[snip]
 
 or have the compiler not complain when it couldn't be 100% certain. If an
 algorithm could be devised which was 100% certain and the warning were
 only given in such cases, then it would likely be acceptable.
In the meantime I have seen you have already commented on the bug report and I agree with with you write there. Thank you. For the In this specific case I think "zero false positives" is doable :-)
It's fine to have warnings which don't find all the instances of a problem, whereas errors _must_ find all of them. However, what isn't fine is having initialization is that they're forced to give you false positives because it's an error to not initialize a local variable, and they can't let anything through that has _any_ possibility of not having been initialized. So, I'd argue that such features are either misfeatures (and the problem should be dealt with another way) or that it should be a warning. Ultimately, what should be avoided with regards to errors and warnings are 1. Errors where you have to deal with false positives because the compiler can't be 100% certain (D has generally dealt with this by doing stuff like default initialization or putting assert(0); at the end of a function rather than doing analysis which isn't 100% certain). 2. Warnings which you cannot get rid of or which force you to do something unreasonable to get rid of them. Having extra warnings which warn you about stuff that the compiler knows is wrong but can't detect 100% of the time shouldn't be a problem as long as the programmer realizes that the compiler can't detect it all of the time. - Jonathan M Davis
Aug 20 2010
prev sibling parent reply Walter Bright <newshound2 digitalmars.com> writes:
bearophile wrote:
 If
 your language (C, D, etc) allows you to not use a variable the last time you
 have initialized it, and then you want to add a warning that finds such
 situations, you don't need the compiler to be 100% accurate, even if some of
 such situations are not detected then it's perfectly OK.
Then you're faced with one of two unpleasant consequences: 1. You have to build the logic of what cases you detect into the specification. This leads to user confusion about the why's of this and gives a bad impression about the arbitrary nature of it. 2. You are faced with errors building with one compiler and no errors on another. I.e. your code loses portability.
Aug 20 2010
parent bearophile <bearophileHUGS lycos.com> writes:
Walter Bright: 
 Then you're faced with one of two unpleasant consequences:
Just a note. If small parts of the type system are with Phobos or user defined D code that used the static introspection I have explained a bit here: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=115833 Then there is no portability problem of the warnings across different compilers, because they are part of the program (or the std lib) so you move them around with the code, across different compilers. Bye, bearophile
Aug 20 2010