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
Feature request: Checks that unless
is not used with multiple conditions.
#5388
Comments
This seems like a good idea. 140 offenses in Rubocop's code is a bad sign though. Maybe it will be too noisy and should be disabled by default? |
Disagree, I think the |
It's a very nice cop! |
That is true. When I said I thought this cop was a good idea, I was thinking developers should extract these more complicated conditions to a method. |
Fixes rubocop#5388 This cop checks that `unless` is not used with multiple conditions. In general, using multiple conditions with `unless` reduces readability.
While looking the code where the offenses were found, I noticed one thing. return unless hash[:a] && hash[:a][:b] I think that this code should not be fixed as follows: return if !hash[:a] || !hash[:a][:b] In such a case, I think that the first example can be used as a kind of idiom. So, I'm wondering whether this cop should be enabled by default... |
cat.pet unless cat.lion? || cat.tiger?
cat.pet unless cat.large? && cat.fangs? These examples both seem perfectly readable. As per @mikegee's suggestion, one can implement |
I agree with it. I think that there are occasionally bad examples, but I understood that there are circumstances to use like this.
In order to avoid this Cop, I think that rewriting of understandable code should be avoided. Based on these, I'm thinking whether to take either of the following:
wdyt? |
I will close this issue once. Thank you very much for giving us your opinion. |
I've been hoping to see this cop for a long time. It's a shame it wasn't merged. Have you had any chance to revisit this issue and, perhaps, think about better impementation? While I agree that not all composite conditions are bad, especially in the multiline approach, I'd love to have an option to warn about conditions that look There's rarely a good reason to use such complex examples, to be honest. I've found that I write this code for two reasons: Compaction. If I have multiple guards, I can turn them into one
I still believe that this code is harder to reason about than the alternative. While the compaction may be good, it's still not as good as the composite if
or even better – some kind of predicate that encapsulates the logic
Avoiding explicit While I believe that this cop should not be auto correctable, it should probably exist in some way |
Agreed!
Just to offer my own $0.02: I've been working with Ruby professionally for ~8 years, through being a Homebrew maintainer for ~12 years and programming professionally (not just using Ruby) for ~14 years and: I find all cases of That's not to say it should be a default cop or whether it should be a style/lint/whatever cop but: I think there is a valid need for a cop like this and it can improve readability (particularly for those coming from languages without an |
It seems there's good reason to think this cop would be useful to people, and the issue was only automatically closed by GitHub when the first PR was closed by it's author, so let's re-open this one. 🙂 |
This is a good idea. The starting point could be to warn against using opposing boolean operators in # ok
return xxx unless a && b && c
return xxx unless a || b || c
# not ok
return xxx unless a && b || c ...
return xxx unless a || b && c ... What should it do if the condition is within return xxx unless a && (b || c) |
I created #9386 to check for mixed use of |
I was worried about the poor readability of code using
unless
with multiple conditions.Looking at the code like the above, I first think about De Morgan's law, rewrite it and proceed with implementation. This is a very painful task.
In order to prevent this problem, I created a cop called
Lint/UnlessMultipleCondtions
.wata727@da4ed99
However, when this Cop is executed to RuboCop, the result is as follows:
I don't mind fixing these problems, but I doubt if this fix will be accepted by the community.
Please tell me your opinion :)
The text was updated successfully, but these errors were encountered: