[erlang-questions] Best practices -- conditional statements

Ivan Uemlianin ivan@REDACTED
Wed Feb 13 14:45:49 CET 2019


Jesper's solution might be even better if you can handle that style.  
Your code base becomes more like a set of rules rather than procedures.  
You especially avoid the nested cases (which I didn't notice in your 
first example).  Nested cases really seem to enforce a procedural view 
of what's going on.

Ivan


On 13/02/2019 13:35, Donald Steven wrote:
> 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
>>
>

-- 
============================================================
Ivan A. Uemlianin PhD
Llaisdy

Ymchwil a Datblygu Technoleg Lleferydd
Speech Technology Research and Development

                     ivan@REDACTED
                         @llaisdy
                          llaisdy.wordpress.com
               github.com/llaisdy
                      www.linkedin.com/in/ivanuemlianin

                         festina lente
============================================================




More information about the erlang-questions mailing list