Notes IV

Jason House jason.james.house at gmail.com
Fri Jan 25 06:29:26 PST 2008


Oskar Linde Wrote:

> bearophile wrote:
> 
> > 18b)
> > Here indentation doesn't follow the program meaning:
> > if (x == 0)
> >     if (y == 0)
> >         foo();
> > else
> >     z = x + y;
> > 
> 
> I've only been bitten by this maybe two times ever, but one was quite 
> recent and took a fair amount of time to track down. (I went as far as 
> reading the compiler assembler output before realizing what was wrong... 
> and feeling quite stupid.)
> 
> I think it went something like starting with
> 
> if (a)
>      b();
> else
>      c();
> 
> and later mindlessly changing several b(); into if(d) b();
> 
> > But Making D *require* {} after for, while, if/else, ecc looks like a "draconian" way to avoid that kind of bugs.
> 
> I agree. A working compromise would be to disallow only "ambiguous" else 
> clauses, forcing one to add {} in those cases only.

My personal style is to use {} after an if/else/while/etc whenever the stuff that follows uses more than one line with normal indentation practices.

For me, I'd never write:
if (x == 0)
    if (y == 0)
        foo();

But would instead write:
if (x == 0){
    if (y == 0)
        foo();
}

Maybe I've just done too much coding/debugging/recoding/sharing of code/etc...  It just seems safer to me.  It also happens to solve the whole else issue.

I wonder if something like this would be an acceptable safety feature in D?  Would anyone complain about the above being an error?  Making it a warning could be a compromise, but Walter dislikes warnings.



More information about the Digitalmars-d mailing list