-
-
Notifications
You must be signed in to change notification settings - Fork 488
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
Extra: add sniff to discourage the use of "long" closures #2311
base: develop
Are you sure you want to change the base?
Conversation
Loosely related to the discussion in 1486. This adds a new sniff to the `WordPress-Extra` ruleset to discourage (`warning`) the use of "long" closures. By default, the sniff will _warn_ when a closure exceeds 5 lines and throw an error when the closure exceeds 8 lines, though these numbers are configurable. By default, the sniff will only count code lines when determining the closure length and will ignore comment-only and blank lines. As the intention is to only discourage long closures, the error has been disabled (by setting it to a really high value). Ref: PHPCSStandards/PHPCSExtra 240 --- :point_right: To discuss: should the value for the `recommendedLines` property stay at `5` (sniff default) or do we want to use a different value ?
This feels more subjective than other items in Extra, even with the option of What do WordPress plugin and theme authors have to gain by having closures limited to a certain number? Why 5, other than it's half of a round number? Is there any external research that says there is a fundamental benefit to reading code with closures that are 5 (or X) lines or less? (Nothing wholly relevant in https://phpmd.org/rules/codesize.html that we could align to.) |
Precisely the reason why it is a separate PR ;-)
Personally, my primary argument would be that it improves the testability of code. Closures cannot be tested on their own and long closures imply higher complexity of the code. Another argument for turning long closures into named functions is documentation/knowledge transfer. Generally speaking, most people do not add a docblock to a closure declaration and documentation related sniffs do not flag this either, while if a closure has higher complexity, it would often be good to document the what and why. Named function declarations lend themselves better for this and we have sniffs in place to enforce docblocks for named functions.
5 is an arbitrary default, though it was chosen with the above mentioned testability argument in mind. (the default for
Readability was not the reason I created the sniff, so I honestly don't know. I didn't do a search to find any readability related research. And while, this PR doesn't solve #1486, it is related to the discussion in that ticket, in particular this bit:
Ref: #1486 (comment) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✅ Approving but this may need a review in the future.
Could you indicate for what ? I mean we are currently reviewing, so I'm happy to discuss any concerns you have. And yes, I do suspect we'll probably get some complaints about this sniff. Still, I think I made the reasoning for adding it clear. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with the reasoning Juliette added, but I'm a bit on the fence about adding this, as it seems a bit arbitrary. I'm not sure if there is a 'good' value for closure length 🤷🏼♂️
But it can easily be turned off in custom rulesets so it solves any issues that people might have (although I doubt we'll have a lot of those).
Saying "no" to this proposal is perfectly acceptable, but in that case, I do think this needs to be further discussed in #1486 as this sniff proposal is linked to that issue. |
I never saw a make post about discouraging 'long' closures to be honest, and since we should always create make posts for these kinds of things, I'd be willing to say no until the issue is agreed upon and then we can just easily add the rule in 3.x version of WPCS. |
Make posts are for things which go into the Having said that, this does (loosely) relate to the following section in the PHP Coding Standards: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php/#closures-anonymous-functions and yes, IIRC that section was added without consultation nor a Make post (and not by us). |
Having re-read 1486, and the associated trac ticket, I'm now more unsure. While I agree that smaller closures have the benefits outlined, and that's generally a good practice, I'm not convinced that it's WPCS's place to push that in this case. We don't have recommendations for function or class lengths, or cyclomatic complexity, and making a suggestion on the number of lines in a Closure feels like those. Like the deprecation notice on https://github.com/object-calisthenics/phpcs-calisthenics-rules says:
|
That notice is unrelated and IMO ill-informed. I mean nearly all WPCS native sniffs are non-code style sniffs, so in what way is PHPCS "only good for handling space and char positions" ? |
We asked for feedback a month ago and haven't received any. What do we want to do with this PR ? Merge it or move it out of the 3.0.0 milestone ? (both are valid options) |
As it's not clear cut, let's leave this one for now. |
I've moved the PR to the "Future release" milestone now. |
Loosely related to the discussion in #1486.
This adds a new sniff to the
WordPress-Extra
ruleset to discourage (warning
) the use of "long" closures.By default, the sniff will warn when a closure exceeds 5 lines and throw an error when the closure exceeds 8 lines, though these numbers are configurable. By default, the sniff will only count code lines when determining the closure length and will ignore comment-only and blank lines.
As the intention is to only discourage long closures, the error has been disabled (by setting it to a really high value).
Ref: PHPCSStandards/PHPCSExtra#240
👉 To discuss: should the value for the
recommendedLines
property stay at5
(sniff default) or do we want to use a different value ?Some input for this based on a run against WP Core (even though the sniff is being added for `Extra` not `Core`):