Idiomatic function names for Phoenix controller helpers

I’m writing some get helpers in my Phoenix web app.

In rails, you would generally name (or have method missing’d) helpers like find_account_by_email(email) , etc.

With pattern matching seeming so core to Elixir/Erlang, I’m wondering if I’m better of writing my helpers like:

def get_account({email: email}) do
  # ...
end

vs

def get_account_by_email(email) do
  # ...
end

Phoenix stubs out a get_account(id) method, so it feels to me that reusing the name with pattern matching is more idiomatic?

4 Likes

I use, probably a bit overuse, the pattern of def blah(bleep: var) do ... end to force me to name some potentially ambiguous arguments and for easy dispatching based on the named arguments.

Typically I would use get_account_by_email(email), and I think in most cases it comes down to personal preferences as opposed to something the Elixir language should encourage (at least in terms of naming).

For the function parameters, I would discourage use of pattern matching on a keyword list in a function, as this can become unwieldily, much like having too many parameters in a function signature. When you pattern match a keyword list, the order must be exactly the same.

I would instead recommend usage of a map, where order doesn’t matter:

def get_account_by(%{email: email})

Which is good when you are just wanting Named Arguments like in OCaml or so, where you want them named but ordered. :slight_smile:

As for unordered, both a kwlist and a map are fairly similar in speed as a map is just a list at smaller map sizes.