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 (ce || e || phras() || bosm() >= 0 || !((groid() || pocken() > 9) && (he || idmi) || !je || psied())) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    erted();
}

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 (((groid() || pocken() > 9) && (he || idmi) || !je || psied()) && bosm() <= 0 && !phras() && !e && !ce) {
    erted();
} 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 (doiGress() != 1 && !oa && fa && !keco || chro && wio && sooRoli() || lia <= 5 || fre && !oa && fa && !keco || chro && wio && sooRoli() || lia <= 5 || hiqol() && !oa && fa && !keco || chro && wio && sooRoli() || lia <= 5) {
    if (!erwi) {
        return true;
    }
}
return false;

Solution

return !erwi || (doiGress() != 1 || fre || hiqol()) && !oa && (fa && (!keco || chro && wio && sooRoli()) || lia <= 5);

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 (!hiqol() && !fre && doiGress() == 1 && erwi) {
    if (oa && erwi) {
        if (!fa && erwi) {
            if (!wio && keco && erwi || !chro && keco && erwi) {
                if (erwi) {
                    return false;
                }
                if (keco) {
                    return false;
                }
                if (!sooRoli()) {
                    return false;
                }
            }
        }
        if (lia >= 5) {
            return false;
        }
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (ze == true) {
    estrit();
}
if (epme > omi && ze != true) {
    nasTreca();
}
if (har == false && ze != true && epme < omi) {
    irdant();
}
if (pe && ze != true && epme < omi && har != false) {
    nadeth();
}
if (ho != axco && ze != true && epme < omi && har != false && !pe) {
    etcrus();
}
if (u > 7 && ze != true && epme < omi && har != false && !pe && ho == axco) {
    pocish();
} else if (fle == true && ze != true && epme < omi && har != false && !pe && ho == axco && u < 7) {
    hangia();
} else if (medd == true && ze != true && epme < omi && har != false && !pe && ho == axco && u < 7 && fle != true) {
    pasmic();
} else if (glol == false && ze != true && epme < omi && har != false && !pe && ho == axco && u < 7 && fle != true && medd != true) {
    anpre();
} else if (od == false && ze != true && epme < omi && har != false && !pe && ho == axco && u < 7 && fle != true && medd != true && glol != false) {
    cosm();
}

Solution

{
    if (ze) {
        estrit();
    }
    if (epme > omi) {
        nasTreca();
    }
    if (!har) {
        irdant();
    }
    if (pe) {
        nadeth();
    }
    if (ho != axco) {
        etcrus();
    }
    if (u > 7) {
        pocish();
    }
    if (fle) {
        hangia();
    }
    if (medd) {
        pasmic();
    }
    if (!glol) {
        anpre();
    }
    if (!od) {
        cosm();
    }
}

Things to double-check in your solution:


Related puzzles: