On Tuesday, 15 July 2014 at 18:08:15 UTC, Martin Krejcirik wrote:

Example?


For loop with multiple variables and various one liners of questionable utility aside:

import std.stdio;

bool funk()
{
    static int count;
    return ++count > 1 ? true : false;
}

void main()
{
    bool flag = false;
    if (flag && funk)
        writeln("a");
    else if (flag=true, flag && funk)
        writeln("b");
    else if (flag && funk)
        writeln("c");
}

cute, but I'd still prefer this

void main()
{
    bool flag = false;
    if (flag && funk)
        writeln("a");
    else
    {
        flag = true;
        if (flag && funk)
            writeln("b");
        else if (flag && funk)
            writeln("c");
    }
}

and not just because I don't like the comma. I'd say it's generally bad practice to hide that write to `flag` inside the if condition. By spreading it out it is clear that the different conditions are evaluated with different external state.

The comma operators entire job is to inject state changes where the reader doesn't expect them. It's a misfeature of C that we've sadly inherited and should rid ourselves from.

Reply via email to