From 7116ec0fe62ad1d13b134c3a963417594a8f04de Mon Sep 17 00:00:00 2001 From: NewAlexandria Date: Sun, 17 Mar 2019 10:57:07 -0400 Subject: [PATCH 1/2] Better clarity on case-when indentation This replaces PR #422, and maintains a minimum of overall new lines --- README.md | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1505d4270..23d6fe8e0 100644 --- a/README.md +++ b/README.md @@ -327,8 +327,12 @@ Translations of the guide are available in the following languages: puts 'Not again!' when song.duration > 120 puts 'Too long!' - when Time.now.hour > 21 - puts "It's too late" + when :minus_op, :minus_minus_op + stack.pop - stack.pop + when MyModule::SomeDomain::BETA_USERS, MyModule::SomeDomain::INTERNAL_RELEASE + stack.pop + stack.pop + when :int_literal, :some_complicate_explicit_name, :contains_musicians_with_arms, :str_interpolated + token.value else song.play end @@ -341,11 +345,26 @@ Translations of the guide are available in the following languages: puts 'Too long!' when Time.now.hour > 21 puts "It's too late" + when :minus_op, :minus_minus_op + stack.pop - stack.pop + when MyModule::SomeDomain::BETA_USERS, + MyModule::SomeDomain::INTERNAL_RELEASE + stack.pop + stack.pop + when :int_literal, + :some_complicate_explicit_name, + :contains_musicians_with_arms, + :str_interpolated + token.value else song.play end ``` + Put multiple when conditions on separate lines, + particularly where the conditions form long, complicated lines. + The 'bad' example also has the issue of causing the entire when line to + diff when only 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. From cb73e3fc08328a8e2d71efe0f8f1e01a148dbf16 Mon Sep 17 00:00:00 2001 From: ZJ Date: Fri, 3 May 2019 13:11:57 -0400 Subject: [PATCH 2/2] case-when uses bad-good-better demo. Simplify --- README.md | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 23d6fe8e0..763f52804 100644 --- a/README.md +++ b/README.md @@ -329,8 +329,6 @@ Translations of the guide are available in the following languages: puts 'Too long!' when :minus_op, :minus_minus_op stack.pop - stack.pop - when MyModule::SomeDomain::BETA_USERS, MyModule::SomeDomain::INTERNAL_RELEASE - stack.pop + stack.pop when :int_literal, :some_complicate_explicit_name, :contains_musicians_with_arms, :str_interpolated token.value else @@ -345,16 +343,6 @@ Translations of the guide are available in the following languages: puts 'Too long!' when Time.now.hour > 21 puts "It's too late" - when :minus_op, :minus_minus_op - stack.pop - stack.pop - when MyModule::SomeDomain::BETA_USERS, - MyModule::SomeDomain::INTERNAL_RELEASE - stack.pop + stack.pop - when :int_literal, - :some_complicate_explicit_name, - :contains_musicians_with_arms, - :str_interpolated - token.value else song.play end @@ -362,9 +350,23 @@ Translations of the guide are available in the following languages: Put multiple when conditions on separate lines, particularly where the conditions form long, complicated lines. - The 'bad' example also has the issue of causing the entire when line to + The 'bad' example also has an issue with code diffs, causing the entire line to diff when only one of the conditions is changed or updated. + + ``` + # better (for multi-condition) + case + when :minus_op, :minus_minus_op + stack.pop - stack.pop + when :int_literal, + :some_complicate_explicit_name, + :contains_musicians_with_arms, + :str_interpolated + token.value + end + ``` + * When assigning the result of a conditional expression to a variable, preserve the usual alignment of its branches.