Elixir 1.3 case assignment

Hi all
I have following code snippet:

defp put_pass_hash(changeset) do
    case changeset do
      %Ecto.Changeset{valid?: true, changes: %{password: pass}} ->
        put_change(changeset, :password_hash, Comeonin.Bcrypt.hashpwsalt(pass))
      _ ->
        changeset
    end
end

In elixir 1.3, it is better to write like?

defp put_pass_hash(changeset) do
    new_changest = case changeset do
      %Ecto.Changeset{valid?: true, changes: %{password: pass}} ->
        put_change(changeset, :password_hash, Comeonin.Bcrypt.hashpwsalt(pass))
      _ ->
        changeset
    end

    new_changest
end

I find it is more clear on the second code snippet. Not?
Thanks

2 Likes

I’m not seeing what is special about the second one other than assigning the case result to a variable, which you could do before Elixir 1.3. What am I missing that is new in 1.3 of the second example?

Nothing about 1.3 affects your former code, and I think your former code is definitely better because it doesn’t create a useless variable.

1 Like

As others have said, both versions are valid in 1.3 as well in earlier versions of elixir, but I have my concerns about both.

If the body of your function is a plain case on an unmodified argument, then this case can almost always be expressed as a toplevel pattern match, which does make the code more readable, but thats only my point of view.

1 Like