[erlang-questions] case or clause

David Budworth dbudworth@REDACTED
Fri Jan 9 03:14:27 CET 2009


is it more useful?
if I have:

f(a,Var) -> ok;
f(b,Var) -> 10/0;
f(c,Var) -> ok.

and call: f(b,foo)

The error message would say it failed in "f/2"
It doesn't tell me at all which one it actually failed in.  Buf for a gen_*
you would know since they show the last message in, which usually lines up
with a function.  But in generic library code, it always seems
hard-to-impossible to tell from the stack.  I end up adding print statements
all over and re-producing to find offending function.

That being said, I actually prefer many little functions anyway as it tends
to be easier to read at a glance.

2009/1/8 Hynek Vychodil <vychodil.hynek@REDACTED>

> I can see one subtle difference between mane short functions and less
> longer. If error occurred, back-trace is more useful for shorter functions.
>
> 2009/1/8 Robert Virding <rvirding@REDACTED>
>
>> This thread is also a comment to an earlier question by Edward Stow <
>> ed.stow@REDACTED>.
>>
>> 2009/1/8 Richard Carlsson <richardc@REDACTED>
>>
>>>
>>> In efficiency terms, there is no real difference. (If there is one,
>>> it should be considered a compiler problem that might be corrected
>>> at any time - do not waste your time on such microoptimizations.)
>>> Oh, and 'if' switches are no different - they're really a 'case'.
>>
>>
>> Just to qualify Richard's comment. There is no difference at all, in the
>> compiler function clauses are first transformed into a case.
>>
>>
>>> So it's mostly a matter of taste. If your switch makes sense on its
>>> own, i.e., you can give it a reasonably straightforward name, then
>>> by all means break it out as a function - it will make the code
>>> easier to read and make it easier for you to see when there is
>>> common functionality that could be reused rather than copy-pasted.
>>
>>
>> Sometimes I also do the opposite and use function clauses / case as a
>> means of grouping to emphasize a code structure. So while you can write:
>>
>> foo(a, m, Z) -> ...;
>> foo(a, n, Z) -> ...;
>> foo(a, o, Z) -> ...
>> ...
>>
>> it might sometimes better clarify your intent to group function clauses
>> and write:
>>
>> foo(a, Y, Z) ->
>>     case Y of
>>         a -> ...;
>>         b -> ...;
>>         ...
>>     end;
>> ...
>>
>> As already noted there is no difference in efficiency. This is probably
>> not a PC thought for a functional language I sometimes feel that you can get
>> too of a good thing and have too many small functions and it can make
>> reading the whole more difficult.
>>
>> Robert
>>
>>
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://www.erlang.org/mailman/listinfo/erlang-questions
>>
>
>
>
> --
> --Hynek (Pichi) Vychodil
>
> Analyze your data in minutes. Share your insights instantly. Thrill your
> boss.  Be a data hero!
> Try Good Data now for free: www.gooddata.com
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20090108/e320dc4a/attachment.htm>


More information about the erlang-questions mailing list