AgentSkillsCN

developing-elixir

全面掌握 Elixir/Phoenix 开发技能,包括 Phoenix 控制器、路由、频道;LiveView 组件与实时功能;Ecto Schema、查询与迁移;OTP 模式(GenServer、Supervisor);函数式领域建模;结合 Absinthe 的 GraphQL;以及 ExUnit 测试。 当您处理 .ex/.exs 文件、Mix 项目、Phoenix 应用程序,或当用户提及 Elixir、Phoenix、Ecto、LiveView、OTP、GenServer、Supervisor、Absinthe,或 ExUnit 时,可选用此方案。此外,当您需要使用 defmodule、mix.exs、伞形应用,或 iex 会话时,也可参考此方案。

SKILL.md
--- frontmatter
name: developing-elixir
description: |
  Comprehensive Elixir/Phoenix development expertise covering Phoenix controllers, routing, channels; LiveView components and real-time features; Ecto schemas, queries, migrations; OTP patterns (GenServer, Supervisor); functional domain modeling; GraphQL with Absinthe; and ExUnit testing.

  Use when working with .ex/.exs files, mix projects, Phoenix applications, or when the user mentions Elixir, Phoenix, Ecto, LiveView, OTP, GenServer, Supervisor, Absinthe, or ExUnit. Also use for defmodule, mix.exs, umbrella apps, or iex sessions.

Developing Elixir

Expert guidance for building robust, scalable Elixir/Phoenix applications.

Quick Start

For immediate help, identify your task type and consult the relevant reference:

Working OnReference FileKey Topics
Controllers, routing, plugs, channelsphoenix-frameworkMVC patterns, authentication, WebSockets
LiveView components, formsliveviewReal-time UI, hooks, streams
Schemas, queries, migrationsecto-databaseAssociations, changesets, transactions
GenServer, Supervisor, processesotp-patternsFault tolerance, state management
Domain modeling, value objectsfunctional-modelingParse-don't-validate, DDD
GraphQL schemas, resolversgraphql-absintheSubscriptions, dataloader
ExUnit tests, TDD workflowtesting-exunitTest organization, assertions

TDD Phase Awareness

All guidance in this skill is phase-aware. Identify your current phase:

RED Phase (Writing Failing Tests)

  • Write the smallest test that captures intent
  • Use hard-coded values directly in tests
  • Skip edge cases and parameterization initially
  • Focus on what behavior the test demands

GREEN Phase (Making Tests Pass)

  • RESIST over-engineering at all costs
  • Start with pure functions and simple data structures
  • Recommend primitives and maps before structs
  • Defer abstractions, smart constructors, and validations
  • Focus only on making the current test pass

REFACTOR Phase (Improving Design)

  • NOW apply proper patterns and abstractions
  • Extract value objects from primitives
  • Add comprehensive error handling
  • Introduce OTP patterns if genuinely needed
  • Improve test structure and coverage

Cross-Cutting Principles

These principles apply across all Elixir development:

Functional-First Approach

  1. Prefer pure functions over stateful processes
  2. Use explicit state passing through function parameters
  3. Only introduce OTP patterns when tests require them
  4. Push side effects to system boundaries

Progressive Abstraction

  1. Start with primitives and maps
  2. Introduce structs when structure is genuinely needed
  3. Add type specs when interfaces stabilize
  4. Extract domain modules when concepts are proven

Parse, Don't Validate

  • Transform unstructured data into guaranteed-valid types at boundaries
  • Once data is parsed, it's always valid throughout the system
  • Prefer constructors that return {:ok, value} | {:error, reason}

Error Handling Philosophy

  • Use tagged tuples consistently: {:ok, result} or {:error, reason}
  • Implement proper error types for domain-specific errors
  • Never expose internal implementation details in errors

Examples

Creating a Phoenix context with Ecto:

code
User: "I need to add a checkout feature for orders"
→ Consult ecto-database.md for schema design, phoenix-framework.md for controller

Implementing real-time updates:

code
User: "Show live order status updates to the customer"
→ Consult liveview.md for socket state and PubSub patterns

Refactoring to proper domain model:

code
User: "This order calculation has grown complex with many edge cases"
→ Identify TDD phase (likely REFACTOR), consult functional-modeling.md

Adding background job processing:

code
User: "Process order confirmations asynchronously"
→ Consult otp-patterns.md for process decisions, phoenix-framework.md for Oban

Anti-Patterns to Avoid

Premature OTP

  • GenServer for data that could be function arguments
  • Supervisors for processes that don't need restart strategies
  • ETS tables for small, static datasets
  • Message passing when direct function calls suffice

Premature Abstraction

  • Creating types for single-use values
  • Building generic solutions for specific problems
  • Introducing abstractions without duplication
  • Modeling future requirements

Testing Anti-Patterns

  • Writing multiple assertions before any pass
  • Creating elaborate test fixtures prematurely
  • Testing implementation details
  • Adding error case tests too early

Reference File IDs

For programmatic access (e.g., parallel reviews), use these identifiers:

phoenix-framework · liveview · ecto-database · otp-patterns · functional-modeling · graphql-absinthe · testing-exunit