AGENT SKILL

Elixir 与 Phoenix 惯用法

elixir-idioms

编写地道的 Elixir 和 Phoenix 代码,运用模式匹配、可组合的小型函数、OTP 友好的边界设计以及 BEAM 感知的错误处理。在创建或审查 Elixir 代码、API、GenServer、Phoenix 应用、Ecto 代码或测试时使用。

使用场景代码实现
License 许可
MIT
最近更新
2026-11-11
Stars
★ 0
Forks
0

01 前言 Front Matter

name
elixir-idioms
description
Write idiomatic Elixir and Phoenix code with pattern matching, small composable functions, OTP-friendly boundaries, and BEAM-aware error handling. Use when creating or reviewing Elixir code, APIs, GenServers, Phoenix apps, Ecto code, or tests.
compatibility
opencode

03 SKILL.md 正文

Use this skill to keep Elixir code assertive, readable, and aligned with core language and OTP conventions.

Mindset

Prefer:

  • Data transformation over object-style mutation
  • Pattern matching over manual branching
  • Small focused functions over large control-flow blocks
  • Explicit return shapes over implicit conventions
  • Supervision and process isolation over defensive over-catching

Write code that feels natural to experienced Elixir developers, not translated from Ruby, JavaScript, or OO languages.

Core style rules

  • Use descriptive module and function names so comments are rarely needed.
  • Keep public APIs small and intention-revealing.
  • Use pipelines when each step clearly transforms the previous value.
  • Do not force everything into a pipeline; break out named variables when branching or meaning improves.
  • Prefer multi-clause functions when clauses represent genuinely different shapes or cases.
  • Keep clauses related; do not use multi-clause functions as a grab bag.

Pattern matching and control flow

Prefer assertive code:

  • Match on expected shapes directly.
  • Let invalid shapes fail early unless the caller truly needs an error tuple.
  • Use guards for real invariants, not as a substitute for better data modeling.

Prefer:

  • case for branching on a value
  • with for linear happy-path flows that short-circuit on failure
  • multiple function clauses for input shape dispatch

Avoid:

  • giant cond or case blocks where private helper functions would clarify intent
  • complex with ... else blocks that normalize many unrelated error shapes in one place

If with needs a complicated else, push normalization closer to the source via small private helpers.

If with needs a lot of different error handlers, use private helpers to handle the error handling, keeping the with tidy.

Data shapes and APIs

  • Use structs when data has a stable shape shared across modules.
  • Use maps for ad hoc or boundary-shaped data.
  • Use keyword lists mainly for optional arguments and configuration.
  • Prefer tagged tuples like {:ok, value} / {:error, reason} for expected operational failures.
  • Raise for programmer errors, impossible states, or misuse of internal APIs.

For maps and structs:

  • Use map.key when the key must exist.
  • Use map[:key] only when the key is optional or dynamic.

Elixir-specific anti-patterns to avoid

  • Do not create atoms from untrusted input with String.to_atom/1.
  • Do not over-comment self-explanatory code.
  • Do not hide invalid data with non-assertive pattern matching.
  • Do not return vague catch-all values when the failure mode matters.
  • Do not pass long unrelated parameter lists when a map/struct would clarify the API.

OTP and process boundaries

When working with processes:

  • Put stateful behavior behind a clear process boundary.
  • Keep GenServers thin; move domain logic into plain modules when possible.
  • Store minimal necessary state.
  • Model sync vs async calls intentionally.
  • Let supervisors own lifecycle and recovery concerns.

Avoid turning GenServers into generic service objects or dumping all business logic into callbacks.

Phoenix and Ecto conventions

In Phoenix code:

  • Keep controllers, LiveViews, and channels thin.
  • Push domain logic into contexts or dedicated modules.
  • Validate and cast external input explicitly.
  • Prefer explicit assigns and event names.

In Ecto code:

  • Use changesets for external data validation and casting.
  • Keep schema modules focused on schema and changeset concerns.
  • Prefer composable query helpers over giant inline queries.
  • Make preload behavior intentional.

Naming and documentation

  • Use ! only for the raising variant of an existing safe operation.
  • Name predicates with ?.
  • Name bangless versions as the default safe API when both forms exist.
  • Use @moduledoc and @doc for public modules and public functions when it helps users of the API.

Testing

  • Prefer ExUnit tests that read like executable examples.
  • Test public behavior first, private helpers indirectly.
  • Pattern match in assertions when shape matters.
  • Use factories/builders that keep test data explicit enough to read.

Review checklist

When writing or reviewing Elixir code, check:

  • Is the data shape clear?
  • Are expected failures modeled explicitly?
  • Is pattern matching doing the heavy lifting?
  • Are functions short and well named?
  • Is OTP used only where state/concurrency/lifecycle actually matter?
  • Does this look like idiomatic Elixir instead of another language wearing Elixir syntax?

04 同仓库更多 Skills

JonasThowsen
JonasThowsen/.dotfiles

技能创建与优化评估

skill-creator

创建新技能、修改并改进现有技能,以及衡量技能性能。当用户想要从头创建技能、编辑或优化现有技能、运行评估(eval)来测试技能、通过方差分析对技能性能做基准测试,或优化技能描述以提高触发准确率时使用。

AI 工程师插件与技能管理
★ 0
JonasThowsen
JonasThowsen/.dotfiles

Astro v6 应用开发

modern-astro-v6

使用服务端优先渲染、islands、content collections(内容集合)、actions 以及当前 v6 安全兼容的 API,构建地道的 Astro v6 应用。在创建、审查或迁移 Astro 项目、页面、组件、内容层或集成时使用该技能。

软件开发工程师前端开发与界面实现
★ 0
JonasThowsen
JonasThowsen/.dotfiles

前端界面设计与实现

frontend-design

创建富有特色、达到生产级且设计精良的前端界面。当用户要求构建 Web 组件、页面、工件、海报或应用时使用此技能(例如网站、落地页、仪表板、React 组件、HTML/CSS 布局,或为任意 Web UI 提供样式美化)。生成富有创意、精致的代码和 UI 设计,避免千篇一律的 AI 风格。

界面设计师前端开发与界面实现
★ 0
JonasThowsen
JonasThowsen/.dotfiles

Effect TypeScript 惯用法

effect-ts-idioms

编写地道的现代 Effect TypeScript 代码,使用 Effect.gen 或清晰的 pipeline(管道)、类型化错误通道、Context 与 Layer 服务,以及资源安全的运行时边界。在使用 Effect、@effect/platform、Schema、Layers 构建或审查代码,或开发基于 Effect 的应用程序时使用。

软件开发工程师代码实现
★ 0
JonasThowsen
JonasThowsen/.dotfiles

Ash 框架惯用法

ash-framework-idioms

编写地道的 Ash 框架代码,使用意图明确的 action(动作)、代码接口、关系管理、DSL 优先的建模,以及框架原生的 change/preparation(变更/准备),而非临时的辅助层。在创建或审查 Ash resource(资源)、domain(域)、action(动作)、query(查询)或 Phoenix 集成时使用。

软件开发工程师代码实现
★ 0