[erlang-questions] Best practices -- conditional statements

Donald Steven t6sn7gt@REDACTED
Wed Feb 13 16:02:07 CET 2019


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

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


More information about the erlang-questions mailing list