Claude-elixir-phoenix elixir-idioms
OTP/BEAM patterns and Elixir idioms — GenServer, Supervisor, Task, Registry, pattern matching, with chains, pipes. Use when designing processes or debugging BEAM issues.
install
source · Clone the upstream repo
git clone https://github.com/oliver-kriska/claude-elixir-phoenix
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/oliver-kriska/claude-elixir-phoenix "$T" && mkdir -p ~/.claude/skills && cp -r "$T/plugins/elixir-phoenix/skills/elixir-idioms" ~/.claude/skills/oliver-kriska-claude-elixir-phoenix-elixir-idioms && rm -rf "$T"
manifest:
plugins/elixir-phoenix/skills/elixir-idioms/SKILL.mdsource content
Elixir Idioms
Reference for writing idiomatic Elixir code with BEAM-aware patterns.
Iron Laws — Never Violate These
- NO PROCESS WITHOUT A RUNTIME REASON — Processes model concurrency, state, isolation—NOT code structure
- MESSAGES ARE COPIED — Keep messages small (except binaries >64 bytes)
- GUARDS USE
/and
/or
— Never use short-circuit operators in guards (guards require boolean operands)not - CHANGESETS FOR EXTERNAL DATA — Use
for user input,cast/4
for internalchange/2 - RESCUE ONLY FOR EXTERNAL CODE — Never use rescue for control flow
- NO DYNAMIC ATOM CREATION —
causes memory leak (atoms aren't GC'd)String.to_atom(user_input) - @external_resource FOR COMPILE-TIME FILES — Modules reading files at compile time MUST declare
@external_resource - SUPERVISE ALL LONG-LIVED PROCESSES — Never bare
/GenServer.start_link
in production. Use supervision treesAgent.start_link - WRAP THIRD-PARTY LIBRARY APIs — Always facade external deps behind a project-owned module. Enables swapping without touching callers
BEAM Architecture (Why Elixir Works This Way)
- Processes are cheap (2.6KB) — Spawn liberally for concurrency/isolation
- Complete memory isolation — No shared state, no locks needed
- Messages are copied (except binaries >64 bytes) — Keep messages small
- Per-process GC — No global GC pauses
- "Let it crash" — Supervisors restart to known-good state
Core Principles
- Pattern match over conditionals — Function heads first, then
, thencasecond - Tagged tuples for expected failures —
/{:ok, _}
for expected errors, raise for bugs{:error, _} - Pipe operator for data transformation — Start with data, never pipe single calls
- Let it crash — Handle expected errors, crash on unexpected ones
- Explicit over implicit — Be clear about intentions
Quick Decision Trees
Control Flow
Need patterns? → case (or function heads) Multiple operations? → with Boolean conditions? → cond (multiple) or if (single)
Error Handling
Expected failure? → {:ok, _}/{:error, _} tuples Unexpected/bug? → raise exception (let supervisor handle) External library? → rescue (only here!)
OTP
Need state? ├─ No → Plain functions ├─ Simple get/update → Agent or ETS ├─ Complex messages/timeouts → GenServer └─ One-off async → Task
Quick Patterns
# Pattern match in function head def process(%{status: :active} = user), do: activate(user) def process(%{status: :inactive} = user), do: deactivate(user) # with for happy path with {:ok, user} <- get_user(id), {:ok, order} <- create_order(user) do {:ok, order} end # Task for async Task.Supervisor.async_nolink(TaskSup, fn -> work() end) |> Task.yield(5000) || Task.shutdown(task)
Common Pitfalls
| Wrong | Right |
|---|---|
| or |
| |
| |
| |
| (unless deprecated in 1.18) |
References
For detailed patterns, see:
- Pattern matching, guards, binary matching${CLAUDE_SKILL_DIR}/references/pattern-matching.md
- GenServer, Supervisor, Task, Registry${CLAUDE_SKILL_DIR}/references/otp-patterns.md
- Tagged tuples, rescue, with${CLAUDE_SKILL_DIR}/references/error-handling.md
- When to use${CLAUDE_SKILL_DIR}/references/with-and-pipes.md
andwith
(idiomatic patterns)|>
- Production BEAM debugging (memory, performance, crashes)${CLAUDE_SKILL_DIR}/references/troubleshooting.md
- Common mistakes and fixes${CLAUDE_SKILL_DIR}/references/anti-patterns.md
- Mix task naming, option parsing, shell output${CLAUDE_SKILL_DIR}/references/mix-tasks.md
- Duration module, dbg improvements (1.18+)${CLAUDE_SKILL_DIR}/references/elixir-118-features.md