agentleFS
zeroasteriskzeroasterisk/skills/elixir-best-practices/SKILL.md

elixir-best-practices

Skill1 starsChanged 3 months agoApache-2.0View on GitHub

Elixir coding standards and best practices. Use when writing, reviewing, or refactoring Elixir code to ensure idiomatic patterns, proper error handling, and Phoenix conventions.

1 other project uses a version of this.

Adopted by

Most recently taken up by

The file

Show its rules and text

We split this skill into the rules it gives an agent

20 rules. About 0 are common, 2 are distinctive, and 18 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 18

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Use with for sequential operations that may fail.

    line 50
  2. Very specific:

    Do NOT add a complex else block to with.

    line 70
  3. Very specific:

    Use the pipe |> for data transformation chains.

    line 74
  4. Very specific:

    Every public function gets a @doc with a doctest.

    line 89
  5. Very specific:

    Use @moduledoc on every module.

    line 108
  6. Very specific:

    Use GenServer for stateful processes, not Agent (Agent is for trivial state)

    line 159
  7. Very specific:

    Prefer Task for one-off async work

    line 160
  8. Very specific:

    Use Supervisor trees — let processes crash and restart

    line 161
  9. Distinctive:

    Never catch exits in production code — let the supervisor handle it

    line 162
  10. Very specific:

    Use Registry for dynamic process lookup, not named processes

    line 163
  11. Very specific:

    Use Ecto.Multi for transactional operations

    line 170
  12. Very specific:

    Use Mox for behavior-based mocking, not module replacement

    line 177
  13. Very specific:

    Use setup blocks for shared fixtures, not helper functions in the test body

    line 179
  14. Very specific:

    Never use String.to_atom/1 with user input (atom table exhaustion)

    line 183
  15. Very specific:

    Never use apply/3 when you can use pattern matching

    line 184
  16. Very specific:

    Never ignore warnings — treat warnings as errors (mix compile --warnings-as-errors)

    line 185
  17. Very specific:

    Never use Enum.each when you need the result — use Enum.map

    line 186
  18. Distinctive:

    Never put logic in templates — extract to functions or components

    line 187
  19. Very specific:

    Never use bare try/rescue — use {:ok, _} / {:error, _} tuples

    line 188
  20. Very specific:

    Never use is_list/1 to check for keyword lists — use Keyword.keyword?/1

    line 189

≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Elixir SWE Agent Instructions

You are a senior Elixir engineer. Write idiomatic, functional Elixir that leverages the language's strengths. Follow these principles strictly.

Pattern Matching Over Conditionals

if/else is a code smell. Use pattern matching, case, multi-clause functions, and guards instead.

# BAD
def process(user) do
  if user.role == :admin do
    admin_action(user)
  else
    regular_action(user)
  end
end

# GOOD
def process(%{role: :admin} = user), do: admin_action(user)
def process(user), do: regular_action(user)

cond is acceptable for complex boolean chains. if/else is acceptable when branching on a boolean value (per Credo's style rules) — use it for true/false checks rather than forcing pattern matching. if without else is fine for simple nil/truthy guards.

# ACCEPTABLE — boolean check, if/else is idiomatic here
if admin?(user) do
  grant_access(user)
else
  deny_access(user)
end

# STILL BAD — structural matching shoved into if/else
if user.role == :admin do
  admin_action(user)
else
  regular_action(user)
end
# Use multi-clause functions or case for structural matches

With for Happy-Path Pipelines

Use with for sequential operations that may fail. Keep the else clause minimal or omit it entirely by letting the non-matching value pass through.

# BAD — nested case
case fetch_user(id) do
  {:ok, user} ->
    case authorize(user) do
      :ok -> do_thing(user)
      error -> error
    end
  error -> error
end

# GOOD
with {:ok, user} <- fetch_user(id),
     :ok <- authorize(user) do
  do_thing(user)
end

Do NOT add a complex else block to with. If you need to handle multiple error types, pattern match in the caller or use a separate function.

Pipe Operator

Use the pipe |> for data transformation chains. The subject flows through. Do NOT pipe into functions where the data isn't the first argument without wrapping.

# GOOD
data
|> parse()
|> validate()
|> persist()

# BAD — pipe into side-effects or multi-arg functions awkwardly
data |> IO.inspect() |> Map.put(map, :key)  # confusing

Documentation and Doctests

Every public function gets a @doc with a doctest. The doctest IS the unit test for the happy path. Write it first.

@doc """
Calculates the total price including tax.

## Examples

    iex> calculate_total(100, 0.1)
    110.0

    iex> calculate_total(0, 0.1)
    0.0
"""
def calculate_total(price, tax_rate) do
  price * (1 + tax_rate)
end

Use @moduledoc on every module. Use @typedoc for public types. No module without a moduledoc.

Structs and Types

Define structs with @enforce_keys for required fields. Always define a @type t() for your structs.

defmodule MyApp.User do
  @moduledoc "A registered user."

  @enforce_keys [:id, :email]
  defstruct [:id, :email, :name, role: :user]

  @type t :: %__MODULE__{
    id: String.t(),
    email: String.t(),
    name: String.t() | nil,
    role: :user | :admin
  }
end

Error Handling

Return {:ok, value} / {:error, reason} tuples. Never raise for expected failures. Reserve raise and ! functions for programmer errors.

# GOOD — tuple returns
def fetch_user(id) do
  case Repo.get(User, id) do
    nil -> {:error, :not_found}
    user -> {:ok, user}
  end
end

# BAD — raising for expected cases
def fetch_user!(id) do
  Repo.get(User, id) || raise "not found"
end

Naming

  • Modules: PascalCase, organized by domain (MyApp.Accounts.User)
  • Functions: snake_case, verb-first for actions (create_user, validate_email)
  • Predicates: end with ? (valid?, admin?)
  • Bang functions: end with ! only for functions that raise (create_user!)
  • Private helpers: prefix with _ only if unused args; use defp for private functions

Process Design

  • Use GenServer for stateful processes, not Agent (Agent is for trivial state)
  • Prefer Task for one-off async work
  • Use Supervisor trees — let processes crash and restart
  • Never catch exits in production code — let the supervisor handle it
  • Use Registry for dynamic process lookup, not named processes

Phoenix Conventions

  • Thin controllers — delegate to context modules
  • Context modules (Accounts, Billing) own the business logic
  • Changesets validate at the boundary — never trust input
  • Use Ecto.Multi for transactional operations
  • LiveView: keep assigns minimal, push computation to the context

Testing

  • Doctests for public API happy paths
  • ExUnit tests for edge cases and error paths
  • Use Mox for behavior-based mocking, not module replacement
  • Async tests by default (async: true) unless they share state
  • Use setup blocks for shared fixtures, not helper functions in the test body

Things to Never Do

  • Never use String.to_atom/1 with user input (atom table exhaustion)
  • Never use apply/3 when you can use pattern matching
  • Never ignore warnings — treat warnings as errors (mix compile --warnings-as-errors)
  • Never use Enum.each when you need the result — use Enum.map
  • Never put logic in templates — extract to functions or components
  • Never use bare try/rescue — use {:ok, _} / {:error, _} tuples
  • Never use is_list/1 to check for keyword lists — use Keyword.keyword?/1

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.