Wikis & learning

Choose the most specific condition first

Order matters when more than one comparison can match the same input. A badge for completed work should take priority over the broader condition for work in progress.

Order matters when more than one comparison can match the same input. A badge for completed work should take priority over the broader condition for work in progress.

Walkthrough

The first true branch ends the chain. Keeping the conditions in descending order prevents a completed checklist from receiving the in-progress label. The fallback covers a checklist with no completed tasks.

PHP example

<?php
foreach ([0, 1, 5] as $done) {
    if ($done >= 5) {
        $label = 'Complete';
    } elseif ($done > 0) {
        $label = 'In progress';
    } else {
        $label = 'New';
    }
    echo $done, ' → ', $label, PHP_EOL;
}

Expected output

0 → New
1 → In progress
5 → Complete

Change and check

Swap the first two conditions and test the boundary values 0, 1 and 5. Explain why the order changes one of the labels.