Skip to content

Commit 7c53f82

Browse files
author
Zachary Jones
committed
add style for multi-condition when-then blocks
1 parent bdde52a commit 7c53f82

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,56 @@ Translations of the guide are available in the following languages:
326326
end
327327
```
328328

329+
<a name="multi-condition-case-when"></a>
330+
* put multiple when conditions on separate lines
331+
particularly where the conditions form long, complicated lines
332+
<sup>[[link](#multi-condition-case-when)]</sup>
333+
334+
```Ruby
335+
# good
336+
337+
case token
338+
when :star_op
339+
stack.pop * stack.pop
340+
when :slash_op
341+
stack.pop / stack.pop
342+
when :minus_op, :minus_minus_op
343+
also_calculate_that
344+
stack.pop - stack.pop
345+
when MyModule::SomeDomain::BETA_USERS,
346+
MyModule::SomeDomain::INTERNAL_RELEASE
347+
stack.pop + stack.pop
348+
when :int_literal,
349+
:some_complicate_explicit_name,
350+
:graduate_borrowers_with_arms,
351+
:str_interpolated
352+
token.value
353+
end
354+
```
355+
356+
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:
357+
358+
```Ruby
359+
# bad
360+
361+
case token
362+
when :star_op
363+
stack.pop * stack.pop
364+
when :slash_op
365+
stack.pop / stack.pop
366+
when :minus_op, :minus_minus_op
367+
also_calculate_that
368+
stack.pop - stack.pop
369+
when MyModule::SomeDomain::BETA_USERS, MyModule::SomeDomain::INTERNAL_RELEASE
370+
stack.pop + stack.pop
371+
when :int_literal, :some_complicate_explicit_name, :graduate_borrowers_with_arms, :str_interpolated
372+
token.value
373+
end
374+
```
375+
376+
The 'bad' example also has the issue of cause the entire when line to diff when one of the conditions is changed or updated
377+
378+
329379
* <a name="indent-conditional-assignment"></a>
330380
When assigning the result of a conditional expression to a variable,
331381
preserve the usual alignment of its branches.

0 commit comments

Comments
 (0)