Dangling if

monarch_dodra monarchdodra at gmail.com
Sun Sep 30 02:42:40 PDT 2012


On Friday, 28 September 2012 at 17:40:17 UTC, Andrej Mitrovic 
wrote:
> On 9/28/12, Bernard Helyer <b.helyer at gmail.com> wrote:
>> By the time the compiler even has a concept of an 'if 
>> statement'
>> or a 'block' the whitespace is long gone. Not to say you
>> couldn't change the lexing model to detect such things,
>> but it's not a simple as you make it sound.
>
> I see, so it's an implementation limitation. I guess we'll have 
> to
> resort to that dlint tool which will have to be built.

Personally, EVEN when I'm doing a 1 line if, I *still* wrap it in 
a block. EG:

if(a == 0)
     a = 1;
or
if(a == 0) a = 1;

Becomes:
if(a == 0)
     {a = 1;}
or
if(a == 0) {a = 1;}

It might look iffy at first, but very quickly feels natural. It 
may look like it requires (god forbid) "useless" typing, but when 
that 1 liner becomes a 2 liner, it saves your life.

It has saved mine more than once actually!

I've done the dangling if bug often. One day I said "no-more!". 
I've addopted the above format, and it has not happened to me 
since.

Further more, thanks to D's ban on "if();", you can litterally 
never fail with this format. I warmly recommend it to every one.


More information about the Digitalmars-d mailing list