[erlang-questions] Trouble with Erlang's lenient comparisons

Torben Hoffmann torben.lehoff@REDACTED
Wed Apr 13 13:26:58 CEST 2011

>From a practical stand point I agree with Kostis.

When we started doing Erlang we used a lot of is_record/2 guards - that
generally makes the code a lot harder to understand, so we dropped that as
we threw away our imperative inheritance.

And the minute you need to take a parameter out of the record it will be
done with the pattern matching anyway, which makes it easier to make changes
in the code.

One learning here: try to do the pattern matching on fields only in the
cases where it is used to pick between different function clauses. It makes
it easier to figure out what is controlling the flow of execution and what
is merely taken out of the parameters to be used as input elsewhere.  We had
a lot of functions that pattern matched a lot of fields in a record, but
only one of the fields were used to control the flow of execution. It was
more clear to do a R#rec.field1 in the function code for the other fields.
But it is a matter of style, of course...


2011/4/13 Håkan Mattsson <hm@REDACTED>

> When you discourage one way of writing code and claim that another way
> is better you ought to motivate why.
> /Håkan
> On Wed, Apr 13, 2011 at 12:36 PM, Kostis Sagonas <kostis@REDACTED>
> wrote:
> > Gordon Guthrie wrote:
> >>
> >> ...
> >>
> >> Also check for bad input to functions by using guards like:
> >>
> >> fun(A, B, C) when is_list(A), is_integer(B), is_record(C, recC) ->
> >
> > The following is a bit pedantic, but please do not offer use of the
> > is_record guard (esp. the one with arity two instead of three) as advice
> to
> > newcomers.  I suggest that you try to forget about this guard's
> existence;
> > the only place where this guard is possibly needed is in or/orelse
> contexts.
> >
> > The above code is written better using pattern matching, as in:
> >
> >  fun(A, B, #recC{} = C) when is_list(A), is_integer(B) ->
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20110413/9c329269/attachment.htm>

More information about the erlang-questions mailing list