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

Suggest case/when if an if/elsif/else can be easily translated #7736

Closed
Lykos opened this issue Feb 20, 2020 · 2 comments · Fixed by #8266
Closed

Suggest case/when if an if/elsif/else can be easily translated #7736

Lykos opened this issue Feb 20, 2020 · 2 comments · Fixed by #8266

Comments

@Lykos
Copy link

Lykos commented Feb 20, 2020

Is your feature request related to a problem? Please describe.

I only learned about the power of case statements quite recently, so there are lots of opportunities in my code that could be written more nicely with a cop that recognizes these patterns. I am sure there are others with the same situation.

Describe the solution you'd like

If there is an if/elsif/else construct with at least one elsif and all conditions can be translated appropriately, suggest using a case/when construct instead.

Additional context

E.g. something like this can be translated:

if a == 1
  # do something
elsif a == 2 || a == 3
   # do something
end

Or something like this:

if a.is_a?(Integer)
  handle_integer(a)
elsif a.is_a?(Float)
  handle_float(a)
end

Note that I might even implement this myself eventually, I'll just add the issue so it's tracked.

@rrosenblum
Copy link
Contributor

I like this idea. This is something that I look for myself in code reviews. I don't think it would be too difficult to implement

@marcandre
Copy link
Contributor

Be mindful about the difference between if a == 1 (can be switched to case) and if a == SomeClass (can't be), or if a == method_call (maybe)

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

Successfully merging a pull request may close this issue.

3 participants