Booleans and conditionals: Correct Solution


Part 1

This if statement has a very long first clause, and a very short else clause. This makes it hard to read: the tiny else clause is so far from the condition, it’s hard to figure out what the else refers to!

if ((marras() && o == 5 && ples() || !(dass() || !fes)) && lece && hild == 2 && (rar && steni() || asos())) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    spei();
}

Improve readability by refactoring this conditional so that its two clauses are swapped: what is now the second clause (the else clause) comes first, and the first clause comes second.

Solution

if (!asos() && (!steni() || !rar) || hild != 2 || !lece || (dass() || !fes) && (!ples() || o != 5 || !marras())) {
    spei();
} else {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
}

Things to double-check in your solution:


Part 2

Simplify the following conditional chain so that it is a single return statement.

if (zi == plal && !al && u < 0 && ed && bantce() && chalau() == unt && trel() || is != 7) {
    if (akswo() && tungre() && chalau() == unt && trel() || is != 7) {
        if (is != 7) {
            if (trel()) {
                return true;
            }
        }
        if (chalau() == unt) {
            return true;
        }
        if (tungre()) {
            return true;
        }
        if (te) {
            return true;
        }
    }
}
return false;

Solution

return ((te || akswo()) && tungre() || zi == plal && !al && u < 0 && ed && bantce()) && chalau() == unt && (trel() || is != 7);

Bonus challenge: rewrite the if/else chain above so that instead of consisting of many return true; statements with one return false; at the end, it has many return false; statements with one return true; at the end.

Solution

if (chalau() != unt || !bantce() && !tungre() || !akswo() && !te || !ed && !tungre() || !akswo() && !te || u > 0 && !tungre() || !akswo() && !te || al && !tungre() || !akswo() && !te || zi != plal && !tungre() || !akswo() && !te) {
    if (!trel()) {
        return false;
    }
    if (is == 7) {
        return false;
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (za == true) {
    hurBism();
} else if (kei == false && za != true) {
    pungsa();
} else if (re == ti && za != true && kei != false) {
    reatan();
}
if (bied == false && za != true && kei != false && re != ti) {
    phun();
} else if (lasm == ac && za != true && kei != false && re != ti && bied != false) {
    stec();
} else if ((oin == a) == true && za != true && kei != false && re != ti && bied != false && lasm != ac) {
    hokte();
}
if (os > 3 && za != true && kei != false && re != ti && bied != false && lasm != ac && (oin == a) != true) {
    astick();
}
if (eohi && za != true && kei != false && re != ti && bied != false && lasm != ac && (oin == a) != true && os < 3) {
    catest();
} else if (or == true && za != true && kei != false && re != ti && bied != false && lasm != ac && (oin == a) != true && os < 3 && !eohi) {
    sloFendic();
} else if (sor < ceu == true && za != true && kei != false && re != ti && bied != false && lasm != ac && (oin == a) != true && os < 3 && !eohi && or != true) {
    ilild();
}

Solution

{
    if (za) {
        hurBism();
    }
    if (!kei) {
        pungsa();
    }
    if (re == ti) {
        reatan();
    }
    if (!bied) {
        phun();
    }
    if (lasm == ac) {
        stec();
    }
    if (oin == a) {
        hokte();
    }
    if (os > 3) {
        astick();
    }
    if (eohi) {
        catest();
    }
    if (or) {
        sloFendic();
    }
    if (sor < ceu) {
        ilild();
    }
}

Things to double-check in your solution:


Related puzzles: