Project

General

Profile

Feature #10343

Updated by sawa (Tsuyoshi Sawada) almost 2 years ago

In a `case` statement, the condition part (`when ...`) in the branches do not have the same length in general, and especially, `else` is much shorter than the conditions. conditions (`when ...`). So when we write the condition and the return value in a single line, they are not aligned, and are hard to read. 

 ```ruby 
 case foo 
   when some_very_long_condition then "a" 
   when short_cond then "bb" 
   when some_long_condition then "ccc" 
   else "dddd" 
 end 
 ``` 

 I propose to allow postfix notations with `when` and `else` (or `otherwise`) inside `case` statement as below: 

 ```ruby 
 case foo 
   "a" when some_very_long_proc 
   "bb" when short_regex 
   "ccc" when some_long_regex 
   "dddd" else 
 end 
 ``` 

 Pros are: 

 1) Postfix notation does not require `then` or `;`, so it is concise. 

 2) The return values from the branches (e.g., `"a"`, `"bb"`, `"ccc"`, `"dddd"`) tend to be shorter and more uniformly lengthened than the conditions, hence they are somewhat close to being aligned naturally, making this easier to read. 

 3) We are usually more interested in the return value than the condition of a branch, especially when we are reading someone's code and are trying to grasp what the `case` statement does or returns. 

 4) This notation is closer to case-like conditional notations regularly used in mathematics: 

     ``` 
          ┌ 1            (x = 0) 
     x! =   
          └ x (x - 1)! (otherwise) 
     ``` 

     So it would be easier to read for those who are familiar with mathematics. 

Back