Skip to content

Multi condition case when #422

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

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,54 @@ Translations of the guide are available in the following languages:
end
```

* <a name="multi-condition-case-when"></a>
Put multiple when conditions on separate lines.
Particularly where the conditions form long, complicated lines.
<sup>[[link](#multi-condition-case-when)]</sup>

```Ruby
# good

case token
when :star_op

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about keeping examples tight and informative? (ruby-style-guide already have more than 3.5k lines of text)
The first 2 similar whens can be merged:

case token
when :star_op
  stack.pop * stack.pop
when :minus_op, :minus_minus_op
  also_calculate_that
  stack.pop - stack.pop
when MyModule::SomeDomain::BETA_USERS,
     MyModule::SomeDomain::INTERNAL_RELEASE
  stack.pop + stack.pop
when :int_literal,
     :some_complicate_explicit_name,
     :graduate_borrowers_with_arms,
     :str_interpolated 
  token.value
end

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is minor / not needed for the example — so yes, sounds well.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the commit diff affect other lines than where the comments are, so these remain. But, updated with 2186b71

stack.pop * stack.pop
when :minus_op, :minus_minus_op
also_calculate_that
stack.pop - stack.pop
when MyModule::SomeDomain::BETA_USERS,
MyModule::SomeDomain::INTERNAL_RELEASE
stack.pop + stack.pop
when :int_literal,
:some_complicate_explicit_name,
:graduate_borrowers_with_arms,
:str_interpolated
token.value
end
```

Though better control of primary domain references should be exercised, this style offers a solution for some 'in the wild' situations. It reads better than:

```Ruby
# bad

case token
when :star_op
stack.pop * stack.pop
when :slash_op
stack.pop / stack.pop
when :minus_op, :minus_minus_op
also_calculate_that
stack.pop - stack.pop
when MyModule::SomeDomain::BETA_USERS, MyModule::SomeDomain::INTERNAL_RELEASE
stack.pop + stack.pop
when :int_literal, :some_complicate_explicit_name, :graduate_borrowers_with_arms, :str_interpolated
token.value
end
```

The 'bad' example also has the issue of cause the entire when line to diff when one of the conditions is changed or updated


* <a name="indent-conditional-assignment"></a>
When assigning the result of a conditional expression to a variable,
preserve the usual alignment of its branches.
Expand Down