[erlang-questions] Which choice is better? Function or Case

Robert Virding robert.virding@REDACTED
Thu Mar 7 19:02:04 CET 2013


Just to explain *why* Garret's example produces the same code. Internally the compiler uses the same code to handle the pattern matching in function clauses and in case clauses so the result is naturally the same. So to reinforce what Garret said: choose that which feels the best.

Some personal comments:

- Sometimes it does feel a bit excessive to create small functions for everything even if it more pure.
- If you want to pattern match on multiple values it looks much better in a separate function.

Robert

----- Original Message -----
> From: "Garrett Smith" <g@REDACTED>
> To: "饕餮" <249505968@REDACTED>
> Cc: "erlang-questions" <erlang-questions@REDACTED>
> Sent: Thursday, 7 March, 2013 5:34:48 PM
> Subject: Re: [erlang-questions] Which choice is better? Function or Case
> 
> On Thu, Mar 7, 2013 at 3:11 AM, 饕餮 <249505968@REDACTED> wrote:
> > I wonder if Function call is better than Case when both of they can
> > implement the target I want?
> > It seems like  write a function instead of case could make the code
> > more
> > clear .
> > But which one is fast when it running?
> > I intend to change the case to function to make the code more
> > clear. Is this
> > valuable?
> 
> Here's a module:
> 
> %% == begin ===========================
> 
> -module(case_vs_function).
> 
> -export([with_case/1, with_function/1]).
> 
> with_case(Msg) ->
>     case Msg of
>         "hello" -> {ok, hi};
>         "goodbye" -> {ok, bye};
>         _ -> error
>     end.
> 
> with_function("hello") -> {ok, hi};
> with_function("goodbye") -> {ok, bye};
> with_function(_) -> error.
> 
> %% == end =============================
> 
> And here's the compiled core Erlang:
> 
> %% == begin ===========================
> 
> module 'case_vs_function' ['module_info'/0,
>                            'module_info'/1,
>                            'with_case'/1,
>                            'with_function'/1]
>     attributes []
> 'with_case'/1 =
>     %% Line 5
>     fun (_cor0) ->
>         %% Line 6
>         case _cor0 of
>           %% Line 7
>           <[104|[101|[108|[108|[111]]]]]> when 'true' ->
>               {'ok','hi'}
>           %% Line 8
>           <[103|[111|[111|[100|[98|[121|[101]]]]]]]> when 'true' ->
>               {'ok','bye'}
>           %% Line 9
>           <_cor3> when 'true' ->
>               'error'
>         end
> 'with_function'/1 =
>     %% Line 12
>     fun (_cor0) ->
>         case _cor0 of
>           <[104|[101|[108|[108|[111]]]]]> when 'true' ->
>               {'ok','hi'}
>           %% Line 13
>           <[103|[111|[111|[100|[98|[121|[101]]]]]]]> when 'true' ->
>               {'ok','bye'}
>           %% Line 14
>           <_cor2> when 'true' ->
>               'error'
>         end
> 'module_info'/0 =
>     fun () ->
>         call 'erlang':'get_module_info'
>             ('case_vs_function')
> 'module_info'/1 =
>     fun (_cor0) ->
>         call 'erlang':'get_module_info'
>             ('case_vs_function', _cor0)
> end
> 
> %% == end =============================
> 
> And the same core Erlang with distractions removed:
> 
> %% == begin ===========================
> 
> 'with_case'/1 =
>     fun (_cor0) ->
>         case _cor0 of
>           <[104|[101|[108|[108|[111]]]]]> when 'true' ->
>               {'ok','hi'}
>           <[103|[111|[111|[100|[98|[121|[101]]]]]]]> when 'true' ->
>               {'ok','bye'}
>           <_cor3> when 'true' ->
>               'error'
>         end
> 
> 'with_function'/1 =
>     fun (_cor0) ->
>         case _cor0 of
>           <[104|[101|[108|[108|[111]]]]]> when 'true' ->
>               {'ok','hi'}
>           <[103|[111|[111|[100|[98|[121|[101]]]]]]]> when 'true' ->
>               {'ok','bye'}
>           <_cor2> when 'true' ->
>               'error'
>         end
> 
> %% == end =============================
> 
> This should alleviate any guilt you may have over using functions,
> instead of case expressions, to make your code more obvious.
> 
> Garrett
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
> 



More information about the erlang-questions mailing list