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 ((frord() <= shadha() && gofe() <= prass() || iped() != e || cirol()) && !(!ulong() || caer) || !herel() && cler && vit <= 8 && (!sa || epen() == 8)) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    nilped();
}

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 ((epen() != 8 && sa || vit >= 8 || !cler || herel()) && (!ulong() || caer || !cirol() && iped() == e && (gofe() >= prass() || frord() >= shadha()))) {
    nilped();
} 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 (rorm && tulsu() && briunt() == 1 && ulso() || ro && mi <= 1 || ioun && acie != shon && cuns > pi && mi <= 1 || uc <= 8 && tulsu() && briunt() == 1 && ulso() || ro && mi <= 1 || ioun && acie != shon && cuns > pi && mi <= 1) {
    if (!oum) {
        return true;
    }
    if (mo == 6) {
        return true;
    }
}
return false;

Solution

return mo == 6 && !oum || (rorm || uc <= 8) && (tulsu() && briunt() == 1 && ulso() || (ro || ioun && acie != shon && cuns > pi) && mi <= 1);

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 (uc >= 8 && !rorm && oum || mo != 6) {
    if (cuns < pi && !ro && !ulso() && oum || mo != 6 || briunt() != 1 && oum || mo != 6 || !tulsu() && oum || mo != 6 || acie == shon && !ro && !ulso() && oum || mo != 6 || briunt() != 1 && oum || mo != 6 || !tulsu() && oum || mo != 6 || !ioun && !ro && !ulso() && oum || mo != 6 || briunt() != 1 && oum || mo != 6 || !tulsu() && oum || mo != 6) {
        if (briunt() != 1 && oum || mo != 6 || !tulsu() && oum || mo != 6) {
            if (mo != 6) {
                if (oum) {
                    return false;
                }
            }
            if (!ulso()) {
                return false;
            }
        }
        if (mi >= 1) {
            return false;
        }
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (aex) {
    fudlon();
}
if (dant == ene && !aex) {
    pasthe();
} else if (si == true && !aex && dant != ene) {
    entIgi();
}
if (arbo && !aex && dant != ene && si != true) {
    osmslo();
}
if (ci == tauc && !aex && dant != ene && si != true && !arbo) {
    bades();
} else if (iro == false && !aex && dant != ene && si != true && !arbo && ci != tauc) {
    wama();
} else if (neba <= 2 && !aex && dant != ene && si != true && !arbo && ci != tauc && iro != false) {
    wiou();
}
if (hu == true && !aex && dant != ene && si != true && !arbo && ci != tauc && iro != false && neba >= 2) {
    luaThash();
}
if (orn == true && !aex && dant != ene && si != true && !arbo && ci != tauc && iro != false && neba >= 2 && hu != true) {
    stasen();
}
if (!tere && !aex && dant != ene && si != true && !arbo && ci != tauc && iro != false && neba >= 2 && hu != true && orn != true) {
    oceJof();
}
if (quss == false && !aex && dant != ene && si != true && !arbo && ci != tauc && iro != false && neba >= 2 && hu != true && orn != true && tere) {
    ishpe();
}

Solution

{
    if (aex) {
        fudlon();
    }
    if (dant == ene) {
        pasthe();
    }
    if (si) {
        entIgi();
    }
    if (arbo) {
        osmslo();
    }
    if (ci == tauc) {
        bades();
    }
    if (!iro) {
        wama();
    }
    if (neba <= 2) {
        wiou();
    }
    if (hu) {
        luaThash();
    }
    if (orn) {
        stasen();
    }
    if (!tere) {
        oceJof();
    }
    if (!quss) {
        ishpe();
    }
}

Things to double-check in your solution:


Related puzzles: