Return from if statements as soon as possible

function foo($bar, $baz) {
    if (!checkSomething($bar, $baz)) {
        if (!findSomething($bar, $baz)) {
            if (!checkFoo($bar, $baz)) {
                if (count($bar) > 0 && $baz == 1) {
                    doSomething();
                }
            }
        }
    }
}

Instead, this is much more readable and manageable:

function foo($bar, $baz) {
    if (checkSomething($bar)) {
        return;
    }

    if (findSomething($bar, $baz)) {
        return;
    }

    if (checkFoo($bar, $baz)) {
        return;
    }

    if (!(count($bar) > 0 && $baz == 1)) {
        return;
    }

    doSomething();
}


Found a typo? Something wrong with this content?

Just fork and edit it.

Content of this work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. Code snippets in examples are published under the CC0 1.0 Universal (CC0 1.0). Thanks to all contributors.