[erlang-questions] erlang sucks

Sean Hinde sean.hinde@REDACTED
Tue Mar 11 23:50:31 CET 2008


On 11 Mar 2008, at 22:36, Nohl Attila Rajmund wrote:

> On Tue, 11 Mar 2008, Ulf Wiger (TN/EAB) wrote:
>
>> attila.rajmund.nohl@REDACTED skrev:
>>>
>>>
>
> I agree that using small functions is a good practice. However, many  
> one
> liner function just makes following the code flow harder, because in  
> the
> end the code is longer. One pattern I often see is:
>
> some_function(X, Y, Z) ->
>     rc(handle_some_function(X, Y, Z)).
>
> rc(ok) ->
>     ok;
>
> rc(error1) ->
>     handle_error1();
>
> rc(error2) ->
>     handle_error2().
>
> I think this is just bloat, it's a lot more elegant even in pesudo-C:
>
> some_function(X, Y, Z) {
>     rc=handle_some_function(X, Y, Z);
>     if (error1==rc) {
>         handle_error1();
>     }
>     else if (error2==rc) {
>         handle_error2();
>     }
> }
>
> It's shorter by only 2 lines, still, my eyes don't have to jump around
> that much when I follow the code. I guess this could be done in erlang
> with if's, but the crusaders against if's have reached our design rule
> document :-)

If rc is a common error handling function for many functions  
throughout the module then I'd say this was OK and serves it's purpose  
well.

The more normal Erlang way to write the equivalent of the C version  
would be simply:

some_function(X,Y,Z) ->
     case handle_some_function(X,Y,Z) of
         ok -> ok;
         error1 -> handle_error1();
         error2 -> handle_error2()
     end.

I'd argue that it is even more readable than the C version. And not an  
'if' in sight :-)

Sean






More information about the erlang-questions mailing list