Skip to content

Conversation

@regularlabs
Copy link
Contributor

Refactors the if structure in Parser::parameters by using an early return (continue in foreach).
This eliminates the use of the else keyword.

Refactors the `if` structure in `Parser::parameters` by using an early return (`continue` in `foreach`).
This eliminates the use of the `else` keyword.
@deleugpn
Copy link
Contributor

deleugpn commented May 2, 2017

Is continue really considered early return approach to replace else? It seems like swapping 6 for half a dozen.

@regularlabs
Copy link
Contributor Author

regularlabs commented May 2, 2017

It has the same logic/reasoning behind it.
You 'exit' the code because what is following it is not applicable anymore.

Compare these 2 approaches.
Loop with an early continue:

foreach ($items as $item) {
    if (! passSomeCheck() ) {
        // do something

        continue;
    }

    // do something else
}

Code extracted to a separate function with an early return:
(This is a better approach when you want to reuse code and/or have more code to do inside the loop)

foreach ($items as $item) {
    doSomething($item)
}

function doSomething($item) {
    if (! passSomeCheck() ) {
        // do something

        return;
    }

    // do something else
}

@taylorotwell
Copy link
Member

Unless it fixes a bug I don't typically merge these kinds of changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants