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 ((mu || an != mehe || !he && wi || !(cino != 2 || no)) && !aeng && na && (!ma || da >= phik)) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    artfar();
}

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 (da <= phik && ma || !na || aeng || (cino != 2 || no) && (!wi || he) && an == mehe && !mu) {
    artfar();
} 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 (phen && smo || tro && smo || nule > meod() || vois() && eunElpun() && fu) {
    if (el && pric > 0) {
        if (vus == deng()) {
            return true;
        }
        if (reosh()) {
            return true;
        }
    }
}
return false;

Solution

return reosh() && vus == deng() || el && pric > 0 || (phen || tro) && smo || nule > meod() || vois() && eunElpun() && fu;

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 (!eunElpun() && nule < meod() && !smo && pric < 0 && vus != deng() || !reosh() || !el && vus != deng() || !reosh() || !tro && !phen && pric < 0 && vus != deng() || !reosh() || !el && vus != deng() || !reosh() || !vois() && nule < meod() && !smo && pric < 0 && vus != deng() || !reosh() || !el && vus != deng() || !reosh() || !tro && !phen && pric < 0 && vus != deng() || !reosh() || !el && vus != deng() || !reosh()) {
    if (!tro && !phen && pric < 0 && vus != deng() || !reosh() || !el && vus != deng() || !reosh()) {
        if (!el && vus != deng() || !reosh()) {
            if (!reosh()) {
                if (vus != deng()) {
                    return false;
                }
            }
            if (pric < 0) {
                return false;
            }
        }
        if (!smo) {
            return false;
        }
    }
    if (nule < meod()) {
        return false;
    }
    if (!fu) {
        return false;
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (doul > 5) {
    ousJemir();
} else if (te == true && doul < 5) {
    paght();
}
if (thi && doul < 5 && te != true) {
    mipin();
} else if (tren == false && doul < 5 && te != true && !thi) {
    esmRar();
} else if (o < 0 && doul < 5 && te != true && !thi && tren != false) {
    codem();
}
if (i == true && doul < 5 && te != true && !thi && tren != false && o > 0) {
    bilred();
}
if (se == true && doul < 5 && te != true && !thi && tren != false && o > 0 && i != true) {
    iapass();
}
if ((iot == 8) == true && doul < 5 && te != true && !thi && tren != false && o > 0 && i != true && se != true) {
    adcass();
}
if (a == true && doul < 5 && te != true && !thi && tren != false && o > 0 && i != true && se != true && (iot == 8) != true) {
    murApid();
}
if (doul < 5 && te != true && !thi && tren != false && o > 0 && i != true && se != true && (iot == 8) != true && a != true) {
    liga();
}

Solution

{
    if (doul > 5) {
        ousJemir();
    }
    if (te) {
        paght();
    }
    if (thi) {
        mipin();
    }
    if (!tren) {
        esmRar();
    }
    if (o < 0) {
        codem();
    }
    if (i) {
        bilred();
    }
    if (se) {
        iapass();
    }
    if (iot == 8) {
        adcass();
    }
    if (a) {
        murApid();
    }
    liga();
}

Things to double-check in your solution:


Related puzzles: