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

code wiget codewiget95@REDACTED
Mon Sep 25 15:25:48 CEST 2017


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!


More information about the erlang-questions mailing list