Dangling if

ixid nuaccount at gmail.com
Mon Oct 1 14:17:00 PDT 2012


On Sunday, 30 September 2012 at 22:56:00 UTC, F i L wrote:
> 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.

I was thinking about this along with optional semi-colons. I 
wonder if it could be done as an editor add-on so you can write D 
like that and then have the plug-in convert it to D that the 
compiler would accept.


More information about the Digitalmars-d mailing list