[erlang-questions] Style question: accessing record fields in a function clause pattern match?
Daniil Churikov
ddosia@REDACTED
Fri Jun 20 09:31:00 CEST 2014
I am trying to stick to the rule: if match header longer then 80 chars then
I move all
the variables bindings to function body, so only necessary to proper
matching parts
will be left.
On Tuesday, June 17, 2014 6:21:27 PM UTC+4, Roger Lipscombe wrote:
>
> I'm currently refactoring some code, and I've got something like this:
>
> handle_foo(#state { type = bar, id = Id }) -> whatever;
> handle_foo(#state { type = baz, parent_id = ParentId }) -> whatever.
>
> ...and I'm wondering whether it's considered bad form to access record
> fields at the same time as matching them in a function clause. I'm
> matching on 'type', and accessing 'id' at the same time, I mean.
>
> Instead, should I consider something like the following?
>
> handle_foo(#state { type = bar }) ->
> Id = State#state.id,
> whatever;
> handle_foo(#state { type = baz }) ->
> ParentId = State#state.parent_id,
> whatever.
>
> The first form makes it explicit that for different types, different
> fields matter (which might be a smell by itself), but it gets hard to
> see the match on 'type' once I'm accessing more than one or two
> fields.
>
> I know this is stylistic, and there's no "right" answer; I just
> wondered what other people tend to do?
>
> Or, more generally, do people prefer using pattern matching to access
> record values, or plain-ol' Foo = State#state.foo expressions?
>
> Thanks,
> Roger.
> _______________________________________________
> erlang-questions mailing list
> erlang-q...@REDACTED <javascript:>
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20140620/3fcc9ac4/attachment.htm>
More information about the erlang-questions
mailing list