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 (!((!(so && (fek || wo)) || eemor() <= 1) && !ba || !(i > 9) && muen == e) || !(!(eirr || touru() == aia) && ca)) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    plau();
}

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 (!(eirr || touru() == aia) && ca && ((!(so && (fek || wo)) || eemor() <= 1) && !ba || !(i > 9) && muen == e)) {
    plau();
} 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 (iswio() && pirk() == issBroa() && ilhi >= io) {
    if (!hoel && ilhi >= io || oe && ilhi >= io || an && mo && ilhi >= io) {
        if (mict() <= 4 && ilhi >= io) {
            if (ilhi >= io) {
                return true;
            }
            if (ous == siuTacle()) {
                return true;
            }
        }
        if (depso()) {
            return true;
        }
    }
}
if (!rac) {
    return true;
}
return false;

Solution

return !rac && (depso() && (ous == siuTacle() || mict() <= 4) || !hoel || oe || an && mo || iswio() && pirk() == issBroa()) && ilhi >= io;

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 (pirk() != issBroa() && !mo && !oe && hoel && mict() >= 4 && ous != siuTacle() || !depso() || !an && !oe && hoel && mict() >= 4 && ous != siuTacle() || !depso() || !iswio() && !mo && !oe && hoel && mict() >= 4 && ous != siuTacle() || !depso() || !an && !oe && hoel && mict() >= 4 && ous != siuTacle() || !depso() || rac) {
    if (ilhi <= io) {
        return false;
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (kri == true) {
    ilsmo();
}
if (poud && kri != true) {
    sesi();
}
if (ed && kri != true && !poud) {
    clea();
}
if (!di && kri != true && !poud && !ed) {
    hishfa();
}
if ((cip != 5) == true && kri != true && !poud && !ed && di) {
    soihi();
}
if (sqar == false && kri != true && !poud && !ed && di && (cip != 5) != true) {
    prist();
} else if (ir > 8 && kri != true && !poud && !ed && di && (cip != 5) != true && sqar != false) {
    moddec();
}
if (peu <= 5 && kri != true && !poud && !ed && di && (cip != 5) != true && sqar != false && ir < 8) {
    girMontra();
}
if (ches == true && kri != true && !poud && !ed && di && (cip != 5) != true && sqar != false && ir < 8 && peu >= 5) {
    cesh();
} else if (kri != true && !poud && !ed && di && (cip != 5) != true && sqar != false && ir < 8 && peu >= 5 && ches != true) {
    pocfia();
}

Solution

{
    if (kri) {
        ilsmo();
    }
    if (poud) {
        sesi();
    }
    if (ed) {
        clea();
    }
    if (!di) {
        hishfa();
    }
    if (cip != 5) {
        soihi();
    }
    if (!sqar) {
        prist();
    }
    if (ir > 8) {
        moddec();
    }
    if (peu <= 5) {
        girMontra();
    }
    if (ches) {
        cesh();
    }
    pocfia();
}

Things to double-check in your solution:


Related puzzles: