[erlang-questions] Best practices -- conditional statements

Jesper Louis Andersen jesper.louis.andersen@REDACTED
Wed Feb 13 14:35:28 CET 2019


On Wed, Feb 13, 2019 at 1:51 PM Ivan Uemlianin <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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20190213/c78999be/attachment.htm>


More information about the erlang-questions mailing list