Skip to content

Commit

Permalink
add style for multi-condition when-then blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Zachary Jones committed Apr 2, 2015
1 parent bdde52a commit 2186b71
Showing 1 changed file with 48 additions and 0 deletions.
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
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

0 comments on commit 2186b71

Please sign in to comment.