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 (acur() >= 9 && !(ar && wrolip() || bu || olha() == henle() && !thel || cheak() && pi >= 0 || pirmot() && fola())) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    cotvel();
}

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 (ar && wrolip() || bu || olha() == henle() && !thel || cheak() && pi >= 0 || pirmot() && fola() || acur() <= 9) {
    cotvel();
} 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 (ocon() && timir() && asza && cas || wube() != cic || iro && atre() && cas || wube() != cic || ir && timir() && asza && cas || wube() != cic || iro && atre() && cas || wube() != cic || knu == deng || apir() < om) {
    if (vior == ehon()) {
        return true;
    }
}
return false;

Solution

return vior == ehon() || (ocon() || ir) && timir() && (asza || iro && atre()) && (cas || wube() != cic) || knu == deng || apir() < om;

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 (!ir && !ocon() && vior != ehon()) {
    if (!timir() && vior != ehon()) {
        if (!atre() && !asza && vior != ehon() || !iro && !asza && vior != ehon()) {
            if (vior != ehon()) {
                return false;
            }
            if (!cas) {
                return false;
            }
            if (wube() == cic) {
                return false;
            }
        }
    }
}
if (knu != deng) {
    return false;
}
if (apir() > om) {
    return false;
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (!am) {
    thriop();
}
if (u == false && am) {
    ents();
}
if (pasm && am && u != false) {
    nengci();
} else if (pe == false && am && u != false && !pasm) {
    edgnen();
}
if ((un == teac) == true && am && u != false && !pasm && pe != false) {
    sontsa();
} else if (leen == true && am && u != false && !pasm && pe != false && (un == teac) != true) {
    ankqan();
}
if (ri == false && am && u != false && !pasm && pe != false && (un == teac) != true && leen != true) {
    posi();
}
if (ma <= 7 && am && u != false && !pasm && pe != false && (un == teac) != true && leen != true && ri != false) {
    spham();
} else if (tu <= feic && am && u != false && !pasm && pe != false && (un == teac) != true && leen != true && ri != false && ma >= 7) {
    chaar();
}
if (wadu && am && u != false && !pasm && pe != false && (un == teac) != true && leen != true && ri != false && ma >= 7 && tu >= feic) {
    metam();
}

Solution

{
    if (!am) {
        thriop();
    }
    if (!u) {
        ents();
    }
    if (pasm) {
        nengci();
    }
    if (!pe) {
        edgnen();
    }
    if (un == teac) {
        sontsa();
    }
    if (leen) {
        ankqan();
    }
    if (!ri) {
        posi();
    }
    if (ma <= 7) {
        spham();
    }
    if (tu <= feic) {
        chaar();
    }
    if (wadu) {
        metam();
    }
}

Things to double-check in your solution:


Related puzzles: