[erlang-questions] Best practices -- conditional statements

Ivan Uemlianin ivan@REDACTED
Wed Feb 13 16:06:36 CET 2019


Some people like it like that.  The repitition on the RHS makes me think 
there's room for more streamlining.

On 13/02/2019 15:02, Donald Steven wrote:
> Alas, that creates a different form of code sprawl, as in:
>
> makePanPositionL(Notes, left2right, [Cur|_]) whenNotes<6->Cur+8;
> makePanPositionL(Notes, left2right, [Cur|_]) whenNotes<12->Cur+4;
> makePanPositionL(Notes, left2right, [Cur|_]) ->Cur+2.
> makePanPositionL(Notes, right2left, [Cur|_]) whenNotes<6->Cur-8;
> makePanPositionL(Notes, right2left, [Cur|_]) whenNotes<12->Cur-4;
> makePanPositionL(Notes, right2left, [Cur|_]) ->Cur-2.
> makePanPositionL(Notes, rapidlyleft2right, [Cur|_]) whenNotes<6->Cur+16;
> makePanPositionL(Notes, rapidlyleft2right, [Cur|_]) whenNotes<12->Cur+8;
> makePanPositionL(Notes, rapidlyleft2right, [Cur|_]) ->Cur+4.
> makePanPositionL(Notes, rapidlyright2left, [Cur|_]) whenNotes<6->Cur-16;
> makePanPositionL(Notes, rapidlyright2left, [Cur|_]) whenNotes<12->Cur-8;
> makePanPositionL(Notes, rapidlyright2left, [Cur|_]) ->Cur-4.
>
>
> On 2/13/2019 8.35 AM, Jesper Louis Andersen wrote:
>> On Wed, Feb 13, 2019 at 1:51 PM Ivan Uemlianin <ivan@REDACTED 
>> <mailto:ivan@REDACTED>> 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.
>>
>>
>> makePanPositionL(Notes, left2right, [Cur|_]) when Notes < 6  -> Cur + 8;
>> makePanPositionL(Notes, left2right, [Cur|_]) when Notes < 12 -> Cur + 4;
>> makePanPositionL(Notes, left2right, [Cur|_])                 -> Cur + 2.
>>
>> You can just lift everything into a pattern match for this piece of 
>> code, which is what I think I'd do. It makes the result far more 
>> tabular, which in some cases is easier to read. There may be other 
>> reasons in the code to not do this, but as it was written, I think 
>> I'd pick the above as the solution. YMMV of course.
>>
>>
>> _______________________________________________
>> 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
============================================================

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20190213/b34f1409/attachment.htm>


More information about the erlang-questions mailing list