[erlang-questions] matching binary against a file

Thomas Lindgren <>
Sat Jun 23 13:30:18 CEST 2007


--- Jeff Rogers <> wrote:

> This does raise a style issue I've wondered about:
> is it better to use 
> guards or match patterns?  Where you have
> 
> read_utf8(N, S, Data) when N > 0 -> .... ;
> read_utf8(0, S, Data) -> ... .
> 
> I would be inclined to use
> 
> read_utf8(0, S, Data) -> ... ;
> read_utf8(N, S, Data) -> ... .
> 
> without using guards.  Is one generally preferred,
> or it is just a style 
> choice?  I assume that the compiler optimizes then
> to pretty much the 
> same thing.

Once upon a time, I think the compiler did a better
job with the first version, but I'm not sure it does
today. So it's mostly a stylistic issue for me. There
are a couple of nits, though:

1. With an explicit test of (N > 0), the two clauses
don't overlap (ie, at most one of them can match for
any input), so you can reorder them freely.
Occasionally useful.

2. And (N > 0) also avoids an infinite loop if you
pass a negative N for some reason. That might not be
applicable in this case.

Best,
Thomas



       
____________________________________________________________________________________
Yahoo! oneSearch: Finally, mobile search 
that gives answers, not web links. 
http://mobile.yahoo.com/mobileweb/onesearch?refer=1ONXIC



More information about the erlang-questions mailing list