This thread is also a comment to an earlier question by Edward Stow <span class="HcCDpe"><span class="lDACoc"><<a href="mailto:ed.stow@gmail.com">ed.stow@gmail.com</a>></span></span>.<br><br><div class="gmail_quote">
2009/1/8 Richard Carlsson <span dir="ltr"><<a href="mailto:richardc@it.uu.se">richardc@it.uu.se</a>></span><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
In efficiency terms, there is no real difference. (If there is one,<br>
it should be considered a compiler problem that might be corrected<br>
at any time - do not waste your time on such microoptimizations.)<br>
Oh, and 'if' switches are no different - they're really a 'case'.</blockquote><div><br>Just to qualify Richard's comment. There is no difference at all, in the compiler function clauses are first transformed into a case. <br>
</div><div> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">So it's mostly a matter of taste. If your switch makes sense on its<br>
own, i.e., you can give it a reasonably straightforward name, then<br>
by all means break it out as a function - it will make the code<br>
easier to read and make it easier for you to see when there is<br>
common functionality that could be reused rather than copy-pasted.</blockquote><div><br>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:<br>
<br>foo(a, m, Z) -> ...;<br>foo(a, n, Z) -> ...;<br>foo(a, o, Z) -> ...<br>...<br><br>it might sometimes better clarify your intent to group function clauses and write:<br><br>foo(a, Y, Z) -><br> case Y of<br>
a -> ...;<br> b -> ...;<br> ...<br> end;<br>...<br></div><br></div>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.<br>
<br>Robert<br><br>