Dangling if

F i L witte2008 at gmail.com
Sun Sep 30 15:56:20 PDT 2012


monarch_dodra wrote:
> 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.

This is exactly why I think the '{}' brackets "should" be a 
requirement and not the '()' brackets:

     if a == b { doSomething(); }

     if a == b
         { doSomething(); }

     if a == b
     {
         doSomething();
         doSomethingElse();
     }

I know this will never happen in D, but it's how it should be, 
IMO.


More information about the Digitalmars-d mailing list