Your Elixir Tips Thread

Oh, one more thing! Apply the “let it fail” philosophy wherever you can!

For example, instead of:

case foo(bar) do
  {:ok, bar} -> {:ok, bar.foo}
  resp -> resp
end

Do no match all clause, and let it fail for unknown scenarios:

case foo(bar) do
  {:ok, bar} -> {:ok, bar.foo}
  {:error, resp} -> {:error, resp}
end

Again, silly example, but what I mean is: exceptions like CaseClauseError are better errors to get than unexpected values escaping out of your own code, believe me.

9 Likes