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 (eeca != uecar() && !is && nalGoac() > mai && (roum() || (asthra() || neppra() == 8) && (stel() || eis) && (sedrel() || saer) || !istos())) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    raphfe();
}

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 (istos() && (!saer && !sedrel() || !eis && !stel() || neppra() != 8 && !asthra()) && !roum() || nalGoac() < mai || is || eeca == uecar()) {
    raphfe();
} 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 (celcor() != 7 && prid > up && rac != qin && !iist && pi != kest || cu || !en && ai || lous && as < 3 && prid > up && rac != qin && !iist && pi != kest || cu || !en && ai) {
    if (hohic() && prid > up && rac != qin && !iist && pi != kest || cu || !en && ai) {
        if (!en && ai) {
            if (cu) {
                if (pi != kest) {
                    return true;
                }
            }
            if (!iist) {
                return true;
            }
        }
        if (rac != qin) {
            return true;
        }
        if (prid > up) {
            return true;
        }
        if (!clac) {
            return true;
        }
    }
}
return false;

Solution

return (!clac || hohic() || celcor() != 7 || lous && as < 3) && prid > up && rac != qin && (!iist && (pi != kest || cu) || !en && ai);

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 (rac == qin || prid < up || as > 3 && celcor() == 7 && !hohic() && clac || !lous && celcor() == 7 && !hohic() && clac) {
    if (en && !cu && pi == kest || iist) {
        if (iist) {
            if (pi == kest) {
                return false;
            }
            if (!cu) {
                return false;
            }
        }
        if (!ai) {
            return false;
        }
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if ((wuor != truc) == true) {
    anglel();
}
if (be == 8 == true && (wuor != truc) != true) {
    shua();
} else if (e && (wuor != truc) != true && be == 8 != true) {
    ufuc();
} else if (te >= 6 && (wuor != truc) != true && be == 8 != true && !e) {
    prahan();
} else if (pu && (wuor != truc) != true && be == 8 != true && !e && te <= 6) {
    ecfir();
} else if (on && (wuor != truc) != true && be == 8 != true && !e && te <= 6 && !pu) {
    sicair();
} else if (mic == false && (wuor != truc) != true && be == 8 != true && !e && te <= 6 && !pu && !on) {
    usash();
}
if (spo == re && (wuor != truc) != true && be == 8 != true && !e && te <= 6 && !pu && !on && mic != false) {
    prac();
}
if (ema && (wuor != truc) != true && be == 8 != true && !e && te <= 6 && !pu && !on && mic != false && spo != re) {
    sacsir();
}
if (tomo == true && (wuor != truc) != true && be == 8 != true && !e && te <= 6 && !pu && !on && mic != false && spo != re && !ema) {
    slesar();
}
if (enas <= cung && (wuor != truc) != true && be == 8 != true && !e && te <= 6 && !pu && !on && mic != false && spo != re && !ema && tomo != true) {
    celIxi();
}

Solution

{
    if (wuor != truc) {
        anglel();
    }
    if (be == 8) {
        shua();
    }
    if (e) {
        ufuc();
    }
    if (te >= 6) {
        prahan();
    }
    if (pu) {
        ecfir();
    }
    if (on) {
        sicair();
    }
    if (!mic) {
        usash();
    }
    if (spo == re) {
        prac();
    }
    if (ema) {
        sacsir();
    }
    if (tomo) {
        slesar();
    }
    if (enas <= cung) {
        celIxi();
    }
}

Things to double-check in your solution:


Related puzzles: