Is this considered good Elixir?

In the Udemy course that I’m using the instructior suggests that a good refactoring is to move some of the function’s code into the parameters of the function:

def pick_color(%Identicon.Image{hex: [r, g, b | _tail]} = image) do
    %Identicon.Image{image | color: {r, g, b}}

That seems sort of confusing to me, but is it considered good form?

Yes, pattern matching of function parameters is very common.

def pick_color(image = %Identicon.Image{hex: [r, g, b | _tail]}) do
  %Identicon.Image{image | color: {r, g, b}}
1 Like

Just curious. What course is that?

The example looks unnecessary complicated for a course. A bit of explanation on pattern-matching and use in parameters:

1 Like

Probably this course…

1 Like

Yes that’s the course, and it’s quite good so far. I think I would prefer to keep the parameters of the function simple so it’s easy to read the signature, and move the reformatting of the parameters down into the function.

The elixir norm is to pattern match in the function params…but to an extent. Really the way I think of it is you want to pattern match on things in the params to validate a format of something. If not you could just do it in the body.

so the above example the function will not match if it does not have hex: that matches… if it was just image in the params you would have to make sure its properly formatted in the function body and that in my opinion is wrong. You normally make multiple function params to handle different cases in an function call.