Prefer case over if-elsif

このページは日本語には対応しておりません。随時翻訳に取り組んでいます。翻訳に関してご質問やご意見ございましたら、お気軽にご連絡ください。

Metadata

ID: ruby-best-practices/case-vs-if-elsif

Language: Ruby

Severity: Info

Category: Best Practices

Description

The rule “Prefer case over if-elsif” is an important guideline for writing cleaner and more readable code in Ruby. The case statement is a multi-way branch statement, similar to if-elsif-else, but it is more concise and clear, especially when comparing a variable to multiple values. It improves the readability of your code and makes it easier to understand and maintain.

The importance of this rule lies in the fact that complex if-elsif-else chains can be difficult to read and understand. It can lead to confusion and increase the chances of introducing bugs in your code. On the other hand, using case makes your code more structured and logical, which is particularly helpful when dealing with multiple conditions.

To avoid violating this rule, you should use case instead of if-elsif whenever you are comparing a variable to multiple different values. In the case statement, you can specify multiple conditions in a more organized manner. If the conditions are not about the same variable or comparison, if-elsif might still be more appropriate.

Non-Compliant Code Examples

if status == :published
  do_something
elsif status == :draft || status == :pending_approval
  do_option_one
else
  do_option_two
end

if status == :published
  do_something
elsif status == :draft
  do_option_one
else
  do_option_two
end

Compliant Code Examples

case status
when :published
  do_something
when :draft, :pending_approval
  do_option_one
else
  do_option_two
end

if status == :published
  do_something
elsif status != :draft
  do_option_one
else
  do_option_two
end

if status1 == :published
  do_something
elsif status2 == :draft
  do_option_one
else
  do_option_two
end
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis