Phpinspectionsea: New pattern: one-time use variable [aggressive behaviour, also fors' inits definition]

Created on 24 Jul 2017  路  17Comments  路  Source: kalessil/phpinspectionsea

$variable = <expression>;
while ($variable) { /* but not $variable comparison expression */
    ...
    $variable = <same expression>;
}

replace with

while ($variable = <expression>) {
    ...
}
enhancement wontfix

Most helpful comment

So, we collected pretty much feedback and it's pretty controversial =)
We'll introduce a new setting: "Aggressive inlining (while, foreach)" disabled by default.

All 17 comments

This does go against "PHP | Probable bugs | Assignment in condition".
Possible to active this pattern based on that inspection's setting?

Nope, we'll need just to generate suppression then with our QF.

What does it means:

Nope, we'll need just to generate suppression then with our QF.

How will it works?

Point aside. My personal experience is that I found my self often breaking apart very long (spaghetti like) conditions in those while, even if I repeat them inside the loop, just for the sake of readability.

From the point of view of performance I don't know whether we gain something though.

I would use it only for simple conditions, fitting in one line of code (~80 chars) for example.

I don't think it is a good idea to use an assignment in this condition. Depending on the expression, it will make the code harder to read. And every developer who is used to avoid assignments in condition will inevitably stumble upon this code and question it.

@davidmpaz

/* @noinspection <Assignment-in-condition-inspection> */
while ($variable = <expression>) {
    ...
}

The inspection would be very questionable, Twitter poll is 50-50 pro and cons as well.
We gaining no performance, but dropping 2 lines of code and introducing assignment in a condition.

@bestform: I agree and tempted at the same time. If my Twitter poll keeps being biased, we'll probably skip this.

Ok got it. Voted already :)

Looks good to me 馃憤

The only bad thing is when the variable has other assignments in the while loop.

Right @drealecs, this noted down now ^_^

I prefer that it be done inside the own looping (maybe cost performance, but it more clear).

while (true) {
    $variable = <expression>;

    if (!$variable) {
        break;
    }
}

@rentalhost : this one is super-controversial =)

So, we collected pretty much feedback and it's pretty controversial =)
We'll introduce a new setting: "Aggressive inlining (while, foreach)" disabled by default.

I know, but it avoid the _assignment-as-expression_ that sounds bad for me, and turn more hard identify a cause of a problem, once that you could confuses about = and ==.

@rentalhost, fully understandable =)

Marking 2.3.10 to review twitter poll results.

Bebo beep, the StaleBot is here. For one year nothing have happened here. It would be great if someone looked into details here within next 21 days when I'll close it.

Was this page helpful?
0 / 5 - 0 ratings