[erlang-questions] Design patterns?

Ulf Wiger ulf@REDACTED
Wed Mar 4 11:49:41 CET 2015


> On 02 Mar 2015, at 20:20, Judson Lester <nyarly@REDACTED> wrote:
> 
> And an open question (for me): when to use case/if versus function heads? Roughly, I've been using cases to reformat the returns of a function call, but the decision trips me up every time.

There is a consideration worth noting that can affect this decision:

While you can’t trace on a given case expression, you *can* trace a function call.

In general, I very much agree with Richard O’Keefe: you should choose whatever best communicates your intent.

However, and perhaps Anders Svensson (now on the OTP team) is one of the most extreme proponents of this view, breaking out things like local funs into separate functions, makes it much easier to debug. This is especially true in live real-time systems, where tracing is one of the few ways you *can* debug (hopefully) without making a mess of things.

Now, Erlang does allow you to selectively trace on anonymous funs, in part since it translates them into actual functions using a consistent naming scheme, but only the nerdiest of Erlang programmers have actually memorized this scheme. So if the code looks like this (example from OTP’s ‘diameter’ application):

merge_service(Opts, Svc) ->
    lists:foldl(fun ms/2, Svc, Opts).

ms({applications, As}, #diameter_service{applications = Apps} = S)
  when is_list(As) ->

…

insert_local_peer(SApps, T, LDict) ->
    lists:foldl(fun(A,D) -> ilp(A, T, D) end, LDict, SApps).

ilp({Id, Alias}, {TC, SA}, LDict) ->
   …

- then you have no problems tracing the ‘inner loop’ function of e.g. a fold.

(Note that in the second example, Anders uses an anonymous fun to include part of the environment.)

From an aesthetic (i.e. communication) standpoint, I find it slightly unfortunate, but OTOH, when it’s consistently and competently done, I will agree that you reasonably quickly get used to ‘decoding’ the style.

For those who want to judge whether it’s a technique they want to adopt, I recommend reading the diameter sources, where it *is* pretty consistently applied.

BR,
Ulf

Ulf Wiger, Co-founder & Developer Advocate, Feuerlabs Inc.
http://feuerlabs.com



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150304/4c66ae83/attachment.htm>


More information about the erlang-questions mailing list