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 (lol && !poen && risur() && (sood() == 3 || !buhe && idspa()) && !(fo == 0 || !(sasi != 5) || hi < 6)) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    oison();
}

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 (fo == 0 || !(sasi != 5) || hi < 6 || (!idspa() || buhe) && sood() != 3 || !risur() || poen || !lol) {
    oison();
} 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 (gomnu() == 0 && e == 7 && edi && tiod || phul() >= 5 || pe <= o && !za || ick || onhi > 0) {
    if (pe <= o && !za || ick || onhi > 0) {
        if (phul() >= 5) {
            if (tiod) {
                return true;
            }
        }
        if (edi) {
            return true;
        }
    }
    if (e == 7) {
        return true;
    }
    if (pa) {
        return true;
    }
}
return false;

Solution

return (pa || gomnu() == 0) && e == 7 && (edi && (tiod || phul() >= 5) || pe <= o && !za || ick || onhi > 0);

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 (e != 7 || gomnu() != 0 && !pa) {
    if (pe >= o && phul() <= 5 && !tiod || !edi) {
        if (!edi) {
            if (!tiod) {
                return false;
            }
            if (phul() <= 5) {
                return false;
            }
        }
        if (za) {
            return false;
        }
    }
    if (!ick) {
        return false;
    }
    if (onhi < 0) {
        return false;
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (prax == true) {
    odbiss();
} else if (co >= olud && prax != true) {
    nebrit();
} else if (se != jes && prax != true && co <= olud) {
    selk();
}
if (ec >= 3 && prax != true && co <= olud && se == jes) {
    cocDaes();
}
if (beco && prax != true && co <= olud && se == jes && ec <= 3) {
    mensud();
} else if (ep == true && prax != true && co <= olud && se == jes && ec <= 3 && !beco) {
    eviSpess();
}
if (i >= bic && prax != true && co <= olud && se == jes && ec <= 3 && !beco && ep != true) {
    prubo();
}
if (ca == true && prax != true && co <= olud && se == jes && ec <= 3 && !beco && ep != true && i <= bic) {
    raran();
} else if (ozi != pran && prax != true && co <= olud && se == jes && ec <= 3 && !beco && ep != true && i <= bic && ca != true) {
    teme();
}

Solution

{
    if (prax) {
        odbiss();
    }
    if (co >= olud) {
        nebrit();
    }
    if (se != jes) {
        selk();
    }
    if (ec >= 3) {
        cocDaes();
    }
    if (beco) {
        mensud();
    }
    if (ep) {
        eviSpess();
    }
    if (i >= bic) {
        prubo();
    }
    if (ca) {
        raran();
    }
    if (ozi != pran) {
        teme();
    }
}

Things to double-check in your solution:


Related puzzles: