[erlang-questions] A style question

Richard O'Keefe <>
Mon Feb 15 01:29:11 CET 2010


The topic is
     F = fun (...) ->
         ...
     end,
     foo(..., F)
vs
     foo(..., fun (...) ->
	...
     end

On Feb 12, 2010, at 10:18 PM, Richard Andrews wrote:
> Clarity. One of my mantras of coding is: "if it's not obviously right
> then it's probably wrong".

We definitely agree about that, which is why I prefer the
second one.  Advantages of the second approach include but
are not limited to:
  - it is immediately obvious that this is the only place
    the function can be called
  - it is immediately obvious by looking up (just as one does for
    code in an 'if' or 'case' or 'receive') what governs the
    execution of this code

These things are not "obviously right" in the separated version.
>
> I find the former preferable when fun() is large or bulky. Inline fun
> can make it unclear what the code is supposed to do.

We are agreed that "large or bulky" code can be unclear.
In specific cases, we might disagree about _which_ chunks of
code should be moved.
>

> The other reason I use the former is to give the fun a useful name
> which (again) makes the code operation/goals clearer (ie.
> code==documentation).

I would rather read your code than the code that made me ask this
question, because the function name was invariably "F" or something
equally unhelpful.

Coming from a Smalltalk background where you write things like
	1 to: n do: [:i |
	  ... loop body ... ]
it's not clear to me that _any_ name would be more useful than a
well chosen comment, but I think we probably agree that "well-chosen"
is more important than whether it's a named function or an inline
one with a comment.

It's probably time to draw this thread to an end.
You and Ulf Wiger have given me something to think about.



More information about the erlang-questions mailing list