From c0cde09844f924f56062f21670ab051894eda3b7 Mon Sep 17 00:00:00 2001 From: buttercat1791 Date: Sun, 15 Feb 2026 00:01:41 -0600 Subject: [PATCH] Add Elixir style rule with code example --- AGENTS.md | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/AGENTS.md b/AGENTS.md index 5346bb8..10e3350 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1,6 +1,6 @@ This is a web application written using the Phoenix web framework. -## Project guidelines +## Project Overview - **Always** run unit tests with `mix test.unit` and autoformat with `mix format` after making edits - Use `mix precommit` alias when you are done with all changes and fix any pending issues @@ -46,6 +46,44 @@ This is a Nostr relay built with Phoenix 1.8 / Elixir. It stores and serves Nost REST API at `/api/events` (show, create, delete) with JSON rendering. Uses `FallbackController` for error handling. +## Development Guidelines + +### Elixir Code Style Rules + +Avoid excessively nested expressions. Prefer named variables to incrementally store results. Example: + +```elixir +# BAD: Deeply nested result expression +def query_events(filter_map) do + with {:ok, filter} <- Filter.from_map(filter_map) do + events = + from(e in Event) + |> Filter.apply(filter) + + {:ok, + Enum.map(events, fn event -> + {:ok, pub_event} = PubEvent.from_db(event) + pub_event + end)} + end +end + +# GOOD: Build the result incrementally with named variables +def query_events(filter_map) when is_map(filter_map) do + with {:ok, filter} <- Filter.from_map(filter_map), + events <- + from(e in Event) + |> Filter.apply(filter), + pub_events <- + Enum.map(events, fn event -> + {:ok, pub_event} = PubEvent.from_db(event) + pub_event + end) do + {:ok, pub_events} + end +end +``` + ### Test Structure - Tests tagged `@moduletag :unit` run with `mix test.unit` (no DB).