[erlang-questions] Best practices -- conditional statements

Donald Steven t6sn7gt@REDACTED
Wed Feb 13 14:35:29 CET 2019


Thanks!  I'm gonna try that.

My multilingual (using elements of Rust and c), heretical fantasy (with 
apologies) is something like:

makePanPositionL(Notes, Mode, L),

     mut CurrentPanPosition = hd(L),

     case Mode of
         left2right ->
             case Notes of
                 < 6   -> CurrentPanPosition += 8;
                 < 12  -> CurrentPanPosition += 4;
                 _else -> CurrentPanPosition += 2
             end;
         right2left ->
             ...

On 2/13/2019 7.51 AM, Ivan Uemlianin wrote:
> This might be cheating but I would avoid the branching altogether if 
> poss., eg:
>
>     PanPosition = CurrentPanPosition + notes_to_pad(Notes),
>     ...
>
>     notes_to_pad(N) where N <  6 -> 8;
>     notes_to_pad(N) where N < 12 -> 4;
>     notes_to_pad(_)              -> 2.
>
> Also, I would avoid assignment inside a conditional, preferring eg:
>
>     PanPosition =
>         case Notes of
> Few when Few < 6 -> CurrentPanPosition + 8;
>             ...
>
> Best wishes
>
> Ivan
>
>
> On 13/02/2019 12:40, Donald Steven wrote:
>> The code excerpt below shows two alternate ways of branching. I'd be 
>> grateful for opinions on which represents best practices. Thanks.
>>
>>
>> Don
>>
>>
>> makePanPositionL(Notes, Mode, L),
>>
>>     CurrentPanPosition = hd(L),
>>
>>     case Mode of
>>
>>         left2right ->
>>             if
>>                 Notes < 6 -> PanPosition = CurrentPanPosition + 8;
>>                 Notes < 12 -> PanPosition = CurrentPanPosition + 4;
>>                 true -> PanPosition = CurrentPanPosition + 2
>>             end;
>>
>>         left2right ->
>>             case Notes of
>>                 Few  when Few  < 6 -> PanPosition = 
>> CurrentPanPosition + 8;
>>                 More when More < 12 -> PanPosition = 
>> CurrentPanPosition + 4;
>>                 _Lots -> PanPosition = CurrentPanPosition + 2
>>             end;
>>
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
>




More information about the erlang-questions mailing list