Skip to content
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

Closed
shalecraig opened this issue Apr 7, 2016 · 8 comments
Closed

Feature Request: Style/EmptyCaseCondition #3019

shalecraig opened this issue Apr 7, 2016 · 8 comments

Comments

@shalecraig
Copy link

Just ran into an unexpected usage of case statements in ruby,

While this works, it's not the most grok-able thing:

def assert_correct_copy(new_version, old_version)
  case
  when new_version.nil?
    # Weird
    raise "This should never happen -- please pass in non-nil"
  when new_version.to_json == old_version.to_json
    # Do nothing.
    log.info("Doing nothing, new_version == old_version")
  else
    actual_implementation(new_version, old_version)
  end
end

In reality, this picks the first truthy thing.

It'd be cleaner to rewrite as an if-statement:

def assert_correct_copy(new_version, old_version)
  if new_version.nil?
    # Weird
    raise "This should never happen -- please pass in non-nil"
  elsif new_version.to_json == old_version.to_json
    # Do nothing.
    log.info("Doing nothing, new_version == old_version")
  else
    actual_implementation(new_version, old_version)
  end
end

Realistically, I'd love a style cop that checks for usages of case that switch on no variable.

That means:

case foo # <- ok
when bar
# ...
case # <- not ok
when self.can_do_action?
# ...
@rrosenblum
Copy link
Contributor

Interesting. I didn't know that you could write a case statement like that. This seems like a rare case, but it is definitely something that RuboCop could check for.

@shalecraig
Copy link
Author

Neither did I! I came across it while reading through some legacy code.

The regex case\n *when works okayish for finding these, but a rubocop's much more elegant than a case statement in this context.

@bbatsov
Copy link
Collaborator

bbatsov commented Apr 10, 2016

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.

@agrimm
Copy link
Contributor

agrimm commented Apr 19, 2016

Potential configurability for those who like the reverse: provide an offense for the use of elsif, telling them to use a case statement instead.

@bolshakov
Copy link
Contributor

As for me, case version is much easier to read

@ghost
Copy link

ghost commented May 21, 2016

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

takkanm added a commit to esminc/deka_eiwakun that referenced this issue May 26, 2016
rubocop 0.40.0 から、条件式無し case がひっかかるようになった。

rubocop/rubocop#3019

しかし、条件無し case の方が、elsif が連続するより
可読性が上がることがあるため、許可したい
takkanm added a commit to esminc/deka_eiwakun that referenced this issue May 26, 2016
rubocop 0.40.0 から、条件式無し case がひっかかるようになった。

rubocop/rubocop#3019

しかし、条件無し case の方が、elsif が連続するより
可読性が上がることがあるため、許可したい
hrysd pushed a commit to esminc/deka_eiwakun that referenced this issue Jun 2, 2016
rubocop 0.40.0 から、条件式無し case がひっかかるようになった。

rubocop/rubocop#3019

しかし、条件無し case の方が、elsif が連続するより
可読性が上がることがあるため、許可したい
Neodelf pushed a commit to Neodelf/rubocop that referenced this issue Oct 15, 2016
drn added a commit to drn/style-guide that referenced this issue Jun 20, 2018
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
drn added a commit to drn/style-guide that referenced this issue Jun 20, 2018
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
@puyo
Copy link

puyo commented Sep 11, 2019

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.

@jking916
Copy link

jking916 commented Apr 6, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants