Skip to content

Commit

Permalink
Use case instead of if-elsifs
Browse files Browse the repository at this point in the history
  • Loading branch information
tejasbubane committed Jul 8, 2019
1 parent 88d20e4 commit 6d7b00b
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions README.adoc
Expand Up @@ -1172,6 +1172,32 @@ result = if some_condition; something else something_else end
result = some_condition ? something : something_else
----

=== `case` vs `if-else` [[case-vs-if-else]]

Prefer `case` over `if-elsif` when compared value is same in each clause.

[source,ruby]
----
# bad
if status == :active
perform_action
elsif status == :inactive || status == :hibernating
check_timeout
else
final_action
end
# good
case status
when :active
perform_action
when :inactive, :hibernating
check_timeout
else
final_action
end
----

=== Use `if`/`case` Returns [[use-if-case-returns]]

Leverage the fact that `if` and `case` are expressions which return a result.
Expand Down

0 comments on commit 6d7b00b

Please sign in to comment.