Created
November 17, 2015 10:34
-
-
Save madlep/d102f5cb1d578064d0c4 to your computer and use it in GitHub Desktop.
which is preferable style: destructuring struct with pattern matching in function definition, or accessing struct members in function body?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Foobar do | |
defstruct :my_field, :another_field | |
def do_a_thing_destructure(%Foobar{my_field: mf, another_field: af}) do | |
something_else(mf, af) | |
end | |
def do_a_thing_struct_access(foobar = %Foobar{}) do | |
something_else(foobar.my_field, foobar.another_field) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I prefer #1. My reasons:
:my_field
and:another_field
, I think it's much better to define those dependencies explicity in the function's signature.This way it's easier to reason about the behaviour of the function.