Elixir practices

Here are our best practices when it comes to Elixir.

Pattern matching

Prefer to pattern match rather than use if conditionals.

def hash_password(%{
  changes: { password: password },
  valid?: true
} = changeset) do
  # hash the password and add it to the changeset
end

def hash_password(%{
  changes: { password: _password },
  valid?: false
} = changeset) do
  # add error to changeset because it is not valid
end

def hash_password(changeset), do: changeset

Prefer GenServer over Agents

Avoid using Agents. Use GenServer instead, since Agent is simply a thin wrapper around GenServer.

Pipe operator

Prefer to only pipe functions when chaining 2 functions or more.

# Avoid: this can easily be written without pipnig
some_value
|> some_function()
# Good
some_function(some_value)
# Also good
some_value
|> some_function()
|> some_other_function()

Linting

Our preferred code health and formatting checker is Credo, used along with the Built in Format Checker. These are integrated to our CI Pipeline.

  mix credo --strict
  mix format --check-formatted