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

Garrett Smith <>
Thu Mar 7 17:34:48 CET 2013


On Thu, Mar 7, 2013 at 3:11 AM, 饕餮 <> 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



More information about the erlang-questions mailing list