Optional braces

bearophile bearophileHUGS at lycos.com
Thu Aug 25 18:32:24 PDT 2011


Walter:

> Nick Sabalausky wrote:
> > Oh god no, don't ban that. I *like* to do that sort of thing:
> 
> Unusual, but legitimate. I think I'll shelve the idea.

This is the code by Nick Sabalausky:

> if(useThisFeature)
> foreach(x; 0..100)
> foreach(y; 0..100)
> if(blah blah long thing)
> if(simple unrelated condition)
> if(hell, maybe even another)
>     statement();


It contains no "else", so it is not influenced by what I was asking for in my enhancement request.

-----------------

By the way, hiding the nesting of the code removing most indents is not a good coding style practice.
So it's better to write it this way, with indents:


if (useThisFeature)
    foreach (x; 0 .. 100)
        foreach (y; 0 .. 100)
            if (blah blah long thing)
                if (simple unrelated condition)
                    if (hell, maybe even another)
                        statement();

If you think those are too many indents, then you are right. But this is the nature of that piece of code. Hiding it doesn't help its understandability.

And you are often able to rewrite it like this:

if (!useThisFeature)
    return;

foreach (x; 0 .. 100)
    foreach (y; 0 .. 100)
        if ((blah blah long thing) &&
            (simple unrelated condition) &&
            (hell, maybe even another))
            statement();

Bye,
bearophile


More information about the Digitalmars-d mailing list