Back to Rules
Web Development

Elixir Phoenix Best Practices: Do's and Don'ts for Cursor AI Coding Rules

Claude Directory November 29, 2025
0 copies 3 downloads

Optimize your Elixir Phoenix projects with these expert Do's and Don'ts. Covering code style, naming, performance, security, LiveView, Ecto, and Tailwind CSS for efficient, idiomatic development.

Rule Content
## Code Style and Structure

**Do:** Craft succinct, idiomatic Elixir code embracing functional paradigms and immutability.
*Example:* Leverage `Enum.reduce` for data transformations: `users |> Enum.reduce(%{}, fn user, acc -> Map.put(acc, user.id, user) end)`.

**Don't:** Rely on mutable state or imperative-style loops.
*Example:* Avoid `for user <- users do ... end` when higher-order functions suffice.

## Naming Conventions

**Do:** Apply snake_case to files, functions, and variables; PascalCase to modules.
*Example:* `defmodule MyApp.Accounts.User` and `def get_user(id) do ... end`.

**Don't:** Mix casing styles or use unclear names.
*Example:* Skip `u` for user; use `current_user` instead.

## Elixir and Phoenix Patterns

**Do:** Harness pattern matching, guards, Phoenix macros, and Ecto for DB ops.
*Example:* `{:ok, user} = Accounts.get_user(id)` with guards like `def user_signed_in?(conn), do: !!conn.assigns.current_user`.

**Don't:** Ignore Phoenix/Ecto conveniences for manual parsing.
*Example:* Avoid raw SQL; use `Repo.preload(user, :posts)`.

## Syntax and Formatting

**Do:** Adhere to Elixir Style Guide, pipe with `|>` , double quotes for strings.
*Example:* `result = input |> String.trim() |> String.downcase()`.

**Don't:** Use single quotes for strings or skip pipes.
*Example:* Not `String.downcase(String.trim(input))`.

## Error Handling and Validation

**Do:** Embrace 'let it crash' with supervisors, Ecto changesets, and flash messages.
*Example:* `changeset = User.changeset(user, params); if changeset.valid?, do: ..., else: error_action(changeset)`.

**Don't:** Catch all errors manually without logging.
*Example:* Skip try/rescue overuse; use supervisors.

## UI and Styling

**Do:** Build dynamic UIs with LiveView and Tailwind for responsive designs; keep templates DRY.
*Example:* `<.live_component module={UserList} id="users"/>` with Tailwind classes like `class="p-4 bg-white shadow rounded-lg"`.

**Don't:** Mix server-rendered and client-side logic unnecessarily.
*Example:* Avoid jQuery; stick to LiveView hooks.

## Performance Optimization

**Do:** Index DB fields, cache with ETS/Redis, preload to prevent N+1.
*Example:* `Repo.all(from u in User, preload: [:posts], where: u.active)`.

**Don't:** Run unoptimized queries or skip caching.
*Example:* Not `for post <- user.posts, do: ...` without preload.

## Key Conventions

**Do:** Use RESTful routes, contexts for logic, GenServers for state, Tasks for jobs.
*Example:* `defmodule MyAppWeb.Router do pipe_through :api; get "/users", UserController, :index end`.

**Don't:** Bloat controllers with business logic.
*Example:* Move to contexts: `Accounts.create_user(params)`.

## Testing

**Do:** Employ ExUnit with TDD and ExMachina for fixtures.
*Example:* `test "lists users", %{conn: conn} do assert html_response(conn, 200) =~ "Users" end`.

**Don't:** Skip tests or hardcode data.
*Example:* Use `build(:user)` over manual maps.

## Security

**Do:** Auth with Guardian/Pow, validate params, guard against XSS/CSRF.
*Example:* `with {:ok, user} <- Accounts.authenticate(params) do ... end`.

**Don't:** Expose raw params or skip CSRF tokens.
*Example:* Always `Phoenix.Controller.action_fallback` for auth fails.

Comments

More Rules

View all
AI/ML

GLM-4.7 Optimized Config & System Prompt Designer

Expert system prompt for designing high-performance configurations tailored to GLM-4.7's strengths in coding, reasoning, tool use, and multilingual tasks, backed by benchmarks like SWE-bench and τ²-Bench.

C
Community
AI/ML

GLM-4.7 Open-Source Coding Expert: Optimized System Prompt

Leverage GLM-4.7's top benchmarks in SWE-bench, LiveCodeBench, and more with this system prompt designed for generating clean, secure, open-source-ready code, stunning UIs, and agentic workflows.

C
Community
AI/ML

GLM-4.7 Optimized Coding Agent

This system prompt transforms an AI into GLM-4.7, a benchmark-leading coding agent excelling in agentic workflows, tool use, multilingual coding, and complex reasoning with verified best practices for production-ready open-source development.

C
Community
DevOps

Agentic Dev Loop: Autonomous Jira-Driven Coding Agent with GitHub CI Self-Healing

Ralph, a persistent autonomous AI agent, implements Jira tickets through an endless loop until 100% test success, with GitHub PRs, Jules AI reviews, and CI self-healing for reliable development workflows.

C
Claude Directory
AI/ML

Türk Hukuku Uzmanı AI Agent: Güvenilir Yasal Danışman System Prompt

Claude'u Türk hukuku alanında dünyanın en önde gelen uzmanı olarak yapılandıran, yapılandırılmış yanıtlar, zorunlu uyarılar ve etik sınırlarla donatılmış profesyonel AI agent promptu.

C
Community
Database

PostgreSQL Best Practices: Expert Subagent Guide

Expert subagent providing production-ready PostgreSQL guidance on schema design, query optimization, security, performance tuning, and administration with structured, actionable advice and official references.

C
Claude Directory