1.2.81. Bail Out Early

When using conditions, it is recommended to quit in the current context, and avoid the else clause altogether.

The main benefit is to make clear the method applies a condition, and stop immediately when this condition is not satisfied. The main sequence is then focused on the important code.

This analysis works with the break, continue, throw and goto keywords too, depending on situations.

<?php

// Bailing out early, low level of indentation
function foo1($a) {
    if ($a > 0) {
        return false;
    }

    $a++;
    return $a;
}

// Works with continue too
foreach($array as $a => $b) {
    if ($a > 0) {
        continue false;
    }

    $a++;
    return $a;
}

// No need for else
function foo2($a) {
    if ($a > 0) {
        return false;
    } else {
        $a++;
    }

    return $a;
}

// No need for else : return goes into then.
function foo3($a) {
    if ($a < 0) {
        $a++;
    } else {
        return false;
    }

    return $a;
}

// Make a return early, and make the condition visible.
function foo3($a) {
    if ($a < 0) {
        $a++;
        methodcall();
        functioncall();
    }
}

?>

See also Avoid nesting too deeply and return early (part 1) and Avoid nesting too deeply and return early (part 2).

1.2.81.1. Suggestions

  • Detect errors, and then, return as soon as possible.

  • When a if…then branches are unbalanced, test for the small branch, finish it with return. Then keep the other branch as the main code.

1.2.81.2. Specs

Short name

Structures/BailOutEarly

Rulesets

All, Analyze, Changed Behavior

Exakat since

0.8.9

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Features

return

Examples

OpenEMR, opencfp

Available in

Entreprise Edition, Exakat Cloud