elixir-idioms
OTP/BEAM patterns and Elixir idioms — GenServer, Supervisor, Task, Registry,
它会碰到什么
扫了多少10 个文本文件,48 KB
它会碰到什么不碰外部(只输出文字)
命中总数1 处
命中统计严重 0 · 高 0 · 中 0 · 低 0
这一栏是扫描器报的事实,不是结论。命中多不等于有毒(安全工具、规则库、示例脚本本来就会包含危险写法),命中少也不等于干净。它和你手上的凭据、文件、网络有什么关系,需要你自己看。
技能内容
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/not— Never use short-circuit operators in guards (guards require boolean operands) - CHANGESETS FOR EXTERNAL DATA — Use
cast/4for user input,change/2for internal - RESCUE ONLY FOR EXTERNAL CODE — Never use rescue for control flow
- NO DYNAMIC ATOM CREATION —
String.to_atom(user_input)causes memory leak (atoms aren't GC'd) - @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/Agent.start_linkin production. Use supervision trees - WRAP THIRD-PARTY LIBRARY APIs — Always facade external deps behind a project-owned module. Enables swapping without touching callers
- MIX TASKS START ONLY WHAT THEY NEED —
Mix.Task.run("app.config")+Application.ensure_all_started/1, neverMix.Task.run("app.start")(boots the FULL tree: endpoint port, Oban consuming) - CAPTURE LOCALE BEFORE SPAWNING — Gettext/CLDR locale is process-local. Read it in the caller and pass explicitly; a spawned Task/GenServer starts with the default locale
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
case, thencond - Tagged tuples for expected failures —
{:ok, _}/{:error, _}for expected errors, raise for bugs - 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 |
|-------|-------|
| length(list) == 0 | list == [] or Enum.empty?(list) |
| list ++ [item] | [item \| list] \|> Enum.reverse() |
| String.to_atom(input) | String.to_existing_atom(input) |
| spawn(fn -> log(conn) end) | ip = conn.ip; spawn(fn -> log(ip) end) |
| unless condition | if !condition (unless deprecated in 1.18) |
References
For detailed patterns, see:
references/pattern-matching.md- Pattern matching, guards, binary matchingreferences/otp-patterns.md- GenServer, Supervisor, Task, Registryreferences/error-handling.md- Tagged tuples, rescue, withreferences/with-and-pipes.md- When to usewithand|>(idiomatic patterns)references/troubleshooting.md- Production BEAM debugging (memory, performance, crashes)references/anti-patterns.md- Common mistakes and fixesreferences/mix-tasks.md- Mix task naming, option parsing, shell outputreferences/elixir-118-features.md- Duration module, dbg improvements (1.18+)references/elixir-120-type-system.md- Gradual type checker,dynamic(), verified bugs as compile warnings (1.20+, OTP 27+)
想直接用这个技能?
本站把开放许可(MIT / Apache 等)的技能按仓库打包整理到网盘,点一下转存到你自己的网盘,不用一个个从 GitHub 拉。许可未声明的技能只给原始仓库链接,不打包。
它属于哪个仓库
星标★ 553
本站分层T2
该仓技能数322
原文件路径
targets/dsh/skills/elixir-idioms/SKILL.md同一个仓库里的其他技能
同名技能的其他版本
有 6 个不同仓库或目录里都有叫 elixir-idioms 的技能。它们内容并不相同,别混用:
- oliver-kriska/claude-elixir-phoenix — OTP/BEAM patterns and Elixir idioms — GenServer, Supervisor, Task, Registry, pattern match
- oliver-kriska/claude-elixir-phoenix — OTP/BEAM patterns and Elixir idioms — GenServer, Supervisor, Task, Registry,
- oliver-kriska/claude-elixir-phoenix — OTP/BEAM patterns and Elixir idioms — GenServer, Supervisor, Task; Use
- oliver-kriska/claude-elixir-phoenix — OTP/BEAM patterns and Elixir idioms — GenServer, Supervisor, Task, Registry,
- oliver-kriska/claude-elixir-phoenix — OTP/BEAM patterns and Elixir idioms — GenServer, Supervisor, Task, Registry,