-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[CS] [Proposal] Replace calls to 0 === count(array())
with !array()
#19182
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
Comments
Dont forget countable objects |
I agree when these are pur array since it can be faster. |
Thanks for pointing that @nicolas-grekas. Are we widely using this notation to allow (now or later) either an |
I don't think we use countables much, still, when count is used, we must be
sure to not break BC, which means this should be thought on a case by case
basis. Not sure it's worth doing now...
|
I'll await for more feedback then, but I don't know if this change should be against |
2.7 if any |
Is there a performaance difference considering counting is not necessary? |
I haven't any benchmark @Tobion, but I can do something about that. |
The results shows that the proposed syntax is much faster than the original: https://3v4l.org/GETTr |
I like this proposal and @phansys showed that it's 4 or 5 times faster than the existing code. But at the same time I don't like this proposal because the code is way harder to read. If I see this: if (0 === count($this->methods)) { } I instantly understand it as "if the array has no elements". But if I see this: if (!$this->methods) { } I understand it as "if this is null", "if this doesn't exist", "if this is false", etc. And "if this has no elements" comes much later. So, if understanding the code as "this has no elements" is important, this change shouldn't be made. |
Im very 👍 on this, basicly we can write faster. @javiereguiluz with a good object domain and understanding of it this should be not so much a problem, ie i read; "If the object has no methods available". (With |
Maybe we could use |
It's still boilerplate code, but definitely better than |
👎:
|
Reconsidered this. I think using Besides that this could lead to counts in loops, and what if the tested array is actually not empty: https://3v4l.org/1sCIf Im not a performance expert, but the gain for the 5.5/5.6 seems significant. So im 👍 for either |
@wouterj, please note that my proposal isn't about a rule for |
At this is exactly what I don't agree on. I mentioned the PHP CS Fixer to illustrate that this isn't a quick and trival change. |
Considering this, I would say stick with |
As the new code is less readable and as the current code does not have a huge performance impact, I'm 👎 as well for this change. On a more general note, when thinking about making similar changes, we should also always think about this bigger impact on Symfony: what value does it bring (value is not just about features, it's also of course about maintainability, consistency, ...)? Is it worth discussing? Is it worth taking the time to do the change, discuss it, merge it, document it, enforce it, ... |
Since empty arrays always evaluates to
false
, these checks can be simplified.An example from
Symfony\Component\Routing\Annotation\Route::setRequirements()
:NOTE: I've found 54 exact matches for
0 === count(
at2.7
branch.The text was updated successfully, but these errors were encountered: