[erlang-questions] Newbie question about Erlang style

Håkan Stenholm hokan.stenholm@REDACTED
Thu Feb 28 00:12:53 CET 2008


Gleber wrote:
> On Wed, Feb 27, 2008 at 10:40 PM, Håkan Stenholm
> <hokan.stenholm@REDACTED> wrote:
>   
>> Convey Christian J NPRI wrote:
>>
>>     
>>> Is there any non-aesthetic reason prefer one of the following approaches over the other?
>>>       
>>  >
>>  >
>>  > if Foo  -> X = 1;
>>  >    true -> X = 2
>>  > end
>>  >
>>  > vs.
>>  >
>>  > X = if
>>  >    Foo  -> 1;
>>  >    true -> 2
>>  >    end
>>  >
>>  >
>>
>>  * "X = if .... end" is generally less cluttered as the variable name
>>  isn't repeated several times, which can be tedious if the variable name
>>  is long.
>>
>>  * This also avoids issues with forgetting to declare the variable in
>>  certain case/if branches.
>>
>>  * It makes it simpler to see where new variables are introduced, as they
>>  will always appear at the beginning of lines.
>>
>>  * The "Variable/Pattern = expression" style makes the code more
>>  consistent with for example the look of function calls. If/when you need
>>  to refactor your expression part into a separate function, there will
>>  then be no need to move variables around:
>>
>>  foo(V) ->   %% ugly style
>>     case V of
>>        foo -> X = 1;
>>        bar -> X = 2
>>     end,
>>     ... X ...
>>
>>  vs
>>
>>  foo(V) -> %% clean style, moving code to foo2/1 is trivial
>>     X = case V of
>>        foo -> 1;
>>        bar -> 2
>>     end,
>>     ... X ...
>>
>>
>>  =>   %% refactored version, compare amount of code that needs to be
>>  moved around
>>
>>  foo(V) ->
>>     X = foo2(V),
>>     ... X ...
>>
>>  foo2(V)
>>     case V of
>>        foo -> 1;
>>        bar -> 2
>>     end.
>>
>>
>>
>>  * It's the way erlang code is usually written.
>>
>>
>>
>>
>>
>>
>>
>>
>>  > Thanks,
>>  > Christian
>>  >
>>  > Christian Convey
>>  > Scientist, Naval Undersea Warfare Centers
>>  > Newport, RI
>>  >
>>  > _______________________________________________
>>  > erlang-questions mailing list
>>  > erlang-questions@REDACTED
>>  > http://www.erlang.org/mailman/listinfo/erlang-questions
>>  >
>>  >
>>  _______________________________________________
>>  erlang-questions mailing list
>>  erlang-questions@REDACTED
>>  http://www.erlang.org/mailman/listinfo/erlang-questions
>>
>>     
>
> Please note that:
>
>   
>>  foo2(V)
>>     case V of
>>        foo -> 1;
>>        bar -> 2
>>     end.
>>     
>
> could be written as
>
> foo2(foo) -> 1;
> foo2(bar) -> 2.
>
> IMHO it is more readable.
>
>   
I agree that it's probably more readable in this case (implicit case and 
fewer line of code), but we could also have something like:

foo2_with_a_realy_stupidly_long_name_long_long_foo(foo) -> 1;
foo2_with_a_realy_stupidly_long_name_long_long_foo(bar) -> 2.

which makes it harder to immediately see the "pattern -> result" mapping 
being done.

The 'case' based version above, was of course simply used because it was 
the simplest way to refactor the code, not because it resulted in the 
simplest code.

--------------------

The use of long function names and return values, combined with a 
standardized document max width (say 80 columns) can also result in ugly 
code:

%% copy & pasted clauses
%% extra lines forced due to limited line width, make results hard to read

foo2_with_a_realy_stupidly_long_name_long_long_foo(foo) ->
    realy_realy_long_answear_atom_1;
foo2_with_a_realy_stupidly_long_name_long_long_foo(bar) ->
    realy_realy_long_answear_atom_2.


%% this could be rewritten as:

%% write long name only once
foo2_with_a_realy_stupidly_long_name_long_long_foo(V) -> foo2(V).

foo2(foo) -> 1;
foo2(bar) -> 2.


%% or

%% avoid inventing yet another function name or
%% having to follow yet another function call
foo2_with_a_realy_stupidly_long_name_long_long_foo(V) ->
    case V of
       foo -> 1;
       bar -> 2
    end.





More information about the erlang-questions mailing list