diff --git a/README.md b/README.md index 5e8da7f34..220a3f0aa 100644 --- a/README.md +++ b/README.md @@ -326,6 +326,54 @@ Translations of the guide are available in the following languages: end ``` +* +Put multiple when conditions on separate lines. +Particularly where the conditions form long, complicated lines. +[[link](#multi-condition-case-when)] + + ```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 + + * When assigning the result of a conditional expression to a variable, preserve the usual alignment of its branches.