Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.5] No need to use a collection for that #20434

Merged
merged 1 commit into from
Aug 6, 2017
Merged

[5.5] No need to use a collection for that #20434

merged 1 commit into from
Aug 6, 2017

Conversation

lucasmichot
Copy link
Contributor

No need to use a collection for that

@browner12
Copy link
Contributor

is there a performance difference here?

@lucasmichot
Copy link
Contributor Author

Yes @browner12 , it's much faster. You don't need to create a useless collection.
Check \Illuminate\Support\Collection::first, it's only calling \Illuminate\Support\Arr::first

@garygreen
Copy link
Contributor

garygreen commented Aug 5, 2017

I'm really not sure why Laravel is so obsessed with Collections so much lately, or closures. Is it for readability? Is it an avoidance of a loop at all costs? Because personally I find the second one to be simpler and more readable.

return ! is_null(Arr::first($dontReport, function ($type) use ($e) {
    return $e instanceof $type;
}));
foreach ($dontReport as $type) {
  if ($e instanceof $type) return true;
}

return false;

@lucasmichot
Copy link
Contributor Author

Yeah @garygreen, but sometimes a function name (first in this case) is also a good indicator about what we are trying to achieve.

At the end anyone can code it the way one wants in any app, but within the framework we should avoid to create useless objects, which is the case here

@vlakoff
Copy link
Contributor

vlakoff commented Aug 5, 2017

Agree with @garygreen. Its second snippet is much more readable. Not to mention it's also much faster.

@taylorotwell taylorotwell merged commit 15841e6 into laravel:master Aug 6, 2017
@lucasmichot lucasmichot deleted the feature/develop/no-collection branch August 7, 2017 09:16
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.

5 participants