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 ((gisim() && fudsa() || !ec) && !(ridmo() && ((!as || pa == 8) && !(adwen() < 9) || !(!pris || epimn())))) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    prae();
}

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 (ridmo() && ((!as || pa == 8) && !(adwen() < 9) || !(!pris || epimn())) || ec && (!fudsa() || !gisim())) {
    prae();
} 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 (felEfeg() || uss || mesca() && dous) {
    if (plon && uciss() && ang != wo && !mu) {
        if (!mu) {
            return true;
        }
        if (ang != wo) {
            return true;
        }
        if (uciss()) {
            return true;
        }
        if (ci != re) {
            return true;
        }
    }
    if (bic > nestuc()) {
        return true;
    }
}
return false;

Solution

return bic > nestuc() && (ci != re || plon) && uciss() && ang != wo && !mu || felEfeg() || uss || mesca() && dous;

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 (!mesca() && !uss && !felEfeg() && mu || ang == wo || !uciss() || !plon && ci == re || bic < nestuc()) {
    if (!uciss() || !plon && ci == re || bic < nestuc()) {
        if (ang == wo) {
            if (mu) {
                return false;
            }
        }
    }
    if (!felEfeg()) {
        return false;
    }
    if (!uss) {
        return false;
    }
    if (!dous) {
        return false;
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (ta == ma) {
    grebra();
} else if (bror == false && ta != ma) {
    prer();
}
if (ea == true && ta != ma && bror != false) {
    veldod();
} else if ((ano == isto) == true && ta != ma && bror != false && ea != true) {
    miiAcdus();
}
if (bi == true && ta != ma && bror != false && ea != true && (ano == isto) != true) {
    ermo();
}
if (a == false && ta != ma && bror != false && ea != true && (ano == isto) != true && bi != true) {
    arceng();
}
if (nati == true && ta != ma && bror != false && ea != true && (ano == isto) != true && bi != true && a != false) {
    flep();
} else if (os >= 8 && ta != ma && bror != false && ea != true && (ano == isto) != true && bi != true && a != false && nati != true) {
    leess();
}
if (ta != ma && bror != false && ea != true && (ano == isto) != true && bi != true && a != false && nati != true && os <= 8) {
    blora();
}

Solution

{
    if (ta == ma) {
        grebra();
    }
    if (!bror) {
        prer();
    }
    if (ea) {
        veldod();
    }
    if (ano == isto) {
        miiAcdus();
    }
    if (bi) {
        ermo();
    }
    if (!a) {
        arceng();
    }
    if (nati) {
        flep();
    }
    if (os >= 8) {
        leess();
    }
    blora();
}

Things to double-check in your solution:


Related puzzles: