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
Add new Style/InvertibleUnlessCondition
cop
#11432
Add new Style/InvertibleUnlessCondition
cop
#11432
Conversation
Style/InvertibleUnlessCondition
cop
I don't really like many of the changes this cop does, so I'd suggest to have it be disabled by default. E.g. I'd never make this change as I don't see any readability gains from it:
I get that some people might dislike |
Instead of completely disabling, we can tweak If you insist on disabling by default, will do that with no problems. |
I think the config makes sense for whoever wants to use the cop (that's why I think the config is fine as it is), but I don't really have issues with Keep in mind that we've been promising people for a while to enable fewer controversial cops (if I could start all over I'd probably disable half of all cops by default), that's why I think it's best to leave it disabled by default as well and leave it to whoever needs it to enable it. |
Sorry, did you mean just to introduce the cop or to also enable it in the rubocop itself (in |
IMO, RuboCop’s codebase doesn't need to be different from the default, so the .rubocop.yml shouldn't be applied differently than the default for the new rule. |
My 2c -- I think this cop should only discourage unless !x # double negation ❌
unless x.none? # double negation ❌
unless x.nonzero? # double negation ❌
unless x # ok ✔️
unless x.any? # ok ✔️
unless x.zero? # ok ✔️ |
@koic yes, it's a somewhat different rule, but I feel it's driven by the same underlying sentiment -- readable code should negate things as few times as possible. 🙂 |
I made a typo and forgot a "not". :-) unless !x # double negation ❌
unless x.none? # double negation ❌
unless x.nonzero? # double negation ❌ Frankly, only the first one reads like a true negation to me. Yeah, you can Funny enough the cop this make the following suggestion to flip an
So here, we actually added a negation and made the predicate heavier to process mentally. |
0364f5d
to
9557516
Compare
Made the cop disabled by default and reverted cop corrections.
It would be easier to process (for me) if the changed version was written like But this is a style cop, so not everyone would like it.
It has a flexible I will be happy to apply more flexibility, if there would be a need. |
module RuboCop | ||
module Cop | ||
module Style | ||
# Checks for usages of `unless` which can be replaced by `if` with inverted condition. |
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.
Probably it's a good idea to mention here that the idea is that code without unless
is easier to read, but that's subjective that's why the cop is disabled by default.
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.
Added.
I'm also wondering if a more generic version of this cop that suggests replacing negated methods with their complementary counterparts won't be useful. |
9557516
to
fafee9c
Compare
There are already |
Yeah, seems I was thinking of |
Closes #11410.
Tested on
rails/rails
- all the offenses looks valid to me.As not everyone would agree that the new version becomes better (I am not one of them, I would rather prefer
if
overunless
in any case), I decided to accept by default cases like the following: