-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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: Style/EmptyCaseCondition #3019
Comments
Interesting. I didn't know that you could write a |
Neither did I! I came across it while reading through some legacy code. The regex |
Yep, that'd be one useful cop. I believe at some point I planned to write it and add a rule to the style guide about this as well. |
Potential configurability for those who like the reverse: provide an offense for the use of |
As for me, case version is much easier to read |
I agree with @bolshakov. This for example: case
when done? then :paid
when expired? then :expired
when expiring? then :expiring
else :pending
end is better than: if done?
:paid
elsif expired?
:expired
elsif expiring?
:expiring
else
:pending
end |
rubocop 0.40.0 から、条件式無し case がひっかかるようになった。 rubocop/rubocop#3019 しかし、条件無し case の方が、elsif が連続するより 可読性が上がることがあるため、許可したい
rubocop 0.40.0 から、条件式無し case がひっかかるようになった。 rubocop/rubocop#3019 しかし、条件無し case の方が、elsif が連続するより 可読性が上がることがあるため、許可したい
rubocop 0.40.0 から、条件式無し case がひっかかるようになった。 rubocop/rubocop#3019 しかし、条件無し case の方が、elsif が連続するより 可読性が上がることがあるため、許可したい
https://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Style/EmptyCaseCondition rubocop/rubocop#3019 (comment) This allows empty case conditions to support the following: case when done? then :paid when expired? then :expired when expiring? then :expiring else :pending end Instead of requiring: if done? :paid elsif expired? :expired elsif expiring? :expiring else :pending end
https://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Style/EmptyCaseCondition rubocop/rubocop#3019 (comment) This allows empty case conditions to support the following: case when done? then :paid when expired? then :expired when expiring? then :expiring else :pending end Instead of requiring: if done? :paid elsif expired? :expired elsif expiring? :expiring else :pending end
Seems very low priority and I can understand both versions, but I do find the elsif version clearer since the empty case is uncommon, it takes me a moment staring at wondering if it's correct, or googling Ruby syntax to double check. @easybills-admin's example is not a fair comparison. It'd be more like: case
when done? then :paid
when expired? then :expired
when expiring? then :expiring
else :pending
end if done? then :paid
elsif expired? then :expired
elsif expiring? then :expiring
else :pending
end Again, to stress, not a priority to me, but I do like the elsif version better. |
FYI - there's an example of an empty case condition in the Ruby style guide: https://rubystyle.guide/#indent-when-to-case. Personally I don't think the empty case condition deserves an offense, I think both methods are readable if done right. |
Just ran into an unexpected usage of case statements in ruby,
While this works, it's not the most grok-able thing:
In reality, this picks the first truthy thing.
It'd be cleaner to rewrite as an if-statement:
Realistically, I'd love a style cop that checks for usages of
case
that switch on no variable.That means:
The text was updated successfully, but these errors were encountered: