Docs: Section on local variables

Jonathan M Davis jmdavisProg at gmx.com
Wed Apr 25 13:03:04 PDT 2012


On Wednesday, April 25, 2012 20:10:18 Stewart Gordon wrote:
> On 25/04/2012 17:10, Andrej Mitrovic wrote:
> > On 4/25/12, Stewart Gordon<smjg_1998 at yahoo.com> wrote:
> >> Even if it's left over from debugging, it
> >> looks silly, and
> >> might lead other people reading the code to believe something's wrong.
> > 
> > There's about a million ways to make code unreadable, and nobody
> > writes pitch-perfect code that has absolutely no leftover code or
> > comments.
> 
> Exactly. But good-quality compilers help programmers in that direction in
> various ways.
> > And what if you're refactoring and you do multiple builds every couple
> > of seconds? You add a variable, remove it, etc etc. Enabling this
> > warning will just make for a noisy compiler.
> 
> But if the spec stays the same, the compiler needs to generate an _error_
> for it in order to conform. If this statement is removed from the spec,
> then it will be a matter of adding a warning. But this is part of why
> warnings are optional in DMD. By enabling warnings in the compiler in the
> first place, the programmer is asking to be informed of things like this.
> 
> > Keeping variables clean
> > is the responsibility of the programmer and not the compiler.
> > 
> > If it doesn't affect the semantics of code the compiler should shut
> > up. Please don't turn the compiler into a reincarnation of Clippy.
> 
> So you think that
> 
> import std.stdio;
> void main() {
> int a, b;
> a + b;
> return;
> writefln("Hello, world!");
> }
> 
> should generate no errors or warnings whatsoever?

The only part of that that I'd want to give an error is what currently gives 
an error - the line with a + b due to the fact that it has no effect. There's 
no reason for such a line to exist even while debugging. But having the 
compiler complain about unused variables and/or unreachable code gets to be 
_really_ annoying when editing code - especially when adding and removing stuff 
during debugging.

Unfortunately, the writefln line _does_ result in an error for unreachable code 
when compiled with -w, but at least it's not an error normally. Still, I'd 
prefer if it weren't even a warning - especially since increasingly I agree 
with Walter's take on warnings (that they shouldn't exist at all - something 
is an error or it isn't; none of this halfway stuff). Warnings are problematic 
in that a good programmer will _never_ leave them in their code and yet so 
many programmers do. So, they become useless noise. The _only_ advantage to 
them is that they can be used for stupid stuff like unreachable code, allowing 
you to leave warnings while editing code but remove them when your done. I 
don't really even like that though, truth be told.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list