[erlang-questions] case or clause

Robert Virding rvirding@REDACTED
Thu Jan 8 23:32:54 CET 2009


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20090108/1766db68/attachment.htm>


More information about the erlang-questions mailing list