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

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

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_function()
# Good
# Also good
|> some_function()
|> some_other_function()


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