[erlang-questions] Nested Case Statements v.s. multiple functions

Daniel Goertzen daniel.goertzen@REDACTED
Mon Sep 25 16:15:21 CEST 2017


You are headed in the right direction.  Two comments on your refactor:

1. Ditch the atom selector.  Just make this part of the function name.
2. You can flatten out the remaining case expressions if you like.  Whether
this is better or not is a matter of opinion, but it definitely makes
tracing easier.

This is where I got with your code...

send_update(Arg1) ->
    send_update_check_indices(...).

send_update_check_indices({ok, [Val1, Val2]}) ->
    send_update_check_values(...);
send_update_check_indices(Else) ->
    lager:error("ERROR: ..").

% and so on



On Mon, Sep 25, 2017 at 8:26 AM code wiget <codewiget95@REDACTED> wrote:

> Hello everyone,
>
> As I get further into Erlang, I am starting to realize that some of my
> functions have been getting pretty ugly with nested case statements. For
> example, I had a nested case statement that looked something like this:
>
> Send_update(Arg1) ->
>         case do this(Arg1) of
>                 {ok, [Val1, Val2]} ->
>                         case do_that(Val1, Val2) of
>                                 {ok, [Val3, Val4]} ->
>                                         case do_this2(…) of
> ….
>
> It continued into this for another few functions, you get the picture -
> its ugly, and it is hard to read.
>
> So I went and I converted it to a top level function that would then call
> lower level functions like so:
>
>
>
>  send_update(Arg1) ->
>      case ... of
>          {ok, [Val1, Val2]} ->
>              send_update(check_indices, {Arg1, Val1, Val2});
>          Else ->
>              lager:error("ERROR: ..")
>      end.
>  send_update(check_indices, {Arg1, Arg2, Arg3}) ->
>      case check_indices(Arg2, Arg3)of
>          true ->
>              send_update(get_values, {Arg1, Arg3});
>          false ->
>              lager:error("EMERGENCY: ….")
>      end;
>  send_update(get_values, {Arg1, Arg2}) ->
>    ...
>      case ... of
>          {ok, [Val1, Val2, VAl3]} ->
>              send_update(send_value, {Arg1, Val1, Val2, Val3});
>          Error ->
>              lager:error("ERROR: …")
>      end;
>  send_update(send_value, {Arg1, Arg2, Arg3, Arg4}) ->
>>      Do_something(Args),
>      ok.
>
>
> Now that I look at it though, both don’t look right. They don’t look like
> something I would write in any other language where I would just have if’s
> and else’s.
>
> Is this the proper way to write Erlang? I know everyone has their own
> style, but I assume there is some accepted form of writing functional
> programs with deep nests.
>
> Thank you for your advice!
> _______________________________________________
> 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/20170925/73a0b18b/attachment.htm>


More information about the erlang-questions mailing list