Dangling if

Jonathan M Davis jmdavisProg at gmx.com
Thu Sep 27 18:58:08 PDT 2012


On Friday, September 28, 2012 03:43:13 Andrej Mitrovic wrote:
> So I just had a bug I thought I'd never have: dpaste.dzfl.pl/5c0ab8b8
> 
> It's pretty obvious what's going on from that code snippet. But in a
> larger codebase where refactoring happens often it's easy to make a
> mistake of leaving out a "dangling" if statement, which is exactly
> what happened here.
> 
> It could be controversial, but maybe we should consider banning the
> use of blank spaces between the beginning of a *non-blocked* if
> statement and the next statement. IOW:
> 
> This is OK:
> if (state) {
> 
> 
> statement 1..
> }
> statement 2..
> 
> This is not ok:
> if (state)
> 
> statement 1..
> statement 2..
> 
> Thoughts?

That would make the language whitespace-sensitive, which I would consider a 
major no-no. Not only do I not particularly like whitespace-sensitivity in 
languages, but from a purely technical point of view it's a huge problem. The 
lexer throws away all whitespace between tokens, so the whitespace which could 
indicate that you screwed up like this is long gone before you have the AST 
necessary to determine that you even have an if statement.

I suppose that you could have the compiler reparse the tokens following the 
parens of an if statement in a whitespace-sensitive manner after it sees that 
you have an if-statement in the AST, but that's getting ugly.

I really don't think that trying to warn people about doing stupid things with 
formatting (be it on purpose or by accident) is something worth attempting. 
It's just not the sort of thing that a whitespace-insensitive language is 
equipped to do.

- Jonathan M Davis


More information about the Digitalmars-d mailing list