mirror of
https://github.com/sanjeed5/awesome-cursor-rules-mdc.git
synced 2026-11-30 23:04:59 +08:00
Regenerate all MDC rules with fresh Exa data and December 2025 best practices
- Refreshed all 241 library rules with current best practices - Updated Exa search results for all libraries with 2025 content - Fixed --regenerate-all flag logic to properly process all libraries - Removed duplicate astro entry in rules.json (merged tags) - Added missing docker.mdc and vim.mdc files - All rules now include modern patterns and anti-patterns - Updated with framework-specific recommendations for 2025 Technical improvements: - Fixed retry_failed_only logic in generate_mdc_files.py - Improved progress tracking for regeneration scenarios - Ensured proper handling of --refresh-exa flag
This commit is contained in:
+360
-268
@@ -1,270 +1,362 @@
|
||||
---
|
||||
description: This rule outlines the best practices and coding standards for developing Elixir applications with the Phoenix framework, covering code organization, performance, security, testing, and common pitfalls.
|
||||
globs: *.ex,*.exs,*.eex,*.leex,*.sface
|
||||
description: Provides definitive guidelines for building robust, maintainable, and performant Phoenix applications using modern Elixir best practices, focusing on context-driven design, strict formatting, and effective testing.
|
||||
globs: **/*
|
||||
---
|
||||
- # Phoenix Framework Best Practices
|
||||
|
||||
This document outlines general best practices for developing Elixir applications with the Phoenix Framework. It aims to promote maintainable, scalable, and performant codebases.
|
||||
|
||||
- ## 1. Code Organization and Structure
|
||||
|
||||
- ### 1.1. Directory Structure Best Practices
|
||||
|
||||
- **`lib/`**: Contains the core application logic, including contexts, schemas, and supporting modules.
|
||||
- **`lib/<app_name>_web/`**: Holds the web-related components, such as controllers, views, templates, channels, and live views.
|
||||
- **`lib/<app_name>/`**: Contains your application’s core domain logic. Favor placing most of your code here.
|
||||
- **`test/`**: Contains all test-related files, including unit, integration, and acceptance tests.
|
||||
- **`priv/repo/migrations/`**: Stores database migration files.
|
||||
- **`config/`**: Contains configuration files for different environments (dev, test, prod).
|
||||
- **`assets/`**: Contains static assets like JavaScript, CSS, and images. Managed by tools like esbuild or Webpack.
|
||||
- **`deps/`**: Automatically generated folder containing project dependencies.
|
||||
- **`rel/`**: Used for building releases with distillery or mix release. Contains configuration for releases.
|
||||
|
||||
- ### 1.2. File Naming Conventions
|
||||
|
||||
- Use `snake_case` for file names (e.g., `user_controller.ex`, `user_schema.ex`).
|
||||
- Match the file name to the module name (e.g., `UserController` module should be in `user_controller.ex`).
|
||||
- For LiveView components, use `_live` suffix (e.g., `user_live.ex`).
|
||||
- For schema files, use `_schema` suffix(e.g. `user_schema.ex`).
|
||||
|
||||
- ### 1.3. Module Organization
|
||||
|
||||
- Organize modules into contexts, representing logical domains within the application (e.g., `Accounts`, `Blog`, `Payments`).
|
||||
- Keep modules small and focused, adhering to the Single Responsibility Principle (SRP).
|
||||
- Use namespaces to group related modules (e.g., `MyApp.Accounts.User`, `MyApp.Blog.Post`).
|
||||
- Favor explicit dependencies between modules to promote clarity and reduce coupling.
|
||||
|
||||
- ### 1.4. Component Architecture (LiveView)
|
||||
|
||||
- Break down complex LiveView pages into smaller, reusable components.
|
||||
- Use functional components (HEEx templates with function definitions) for simple UI elements.
|
||||
- Use LiveComponent modules for more complex, stateful components with event handling.
|
||||
- Organize components into directories based on their purpose or domain.
|
||||
- Define attributes and slots for LiveView components to ensure type safety.
|
||||
- Utilize PubSub for communication between LiveView components.
|
||||
|
||||
- ### 1.5. Code Splitting Strategies
|
||||
|
||||
- Separate concerns by using contexts. Controllers delegate to contexts, which handle the business logic, and contexts rely on schemas, which handle the data validations and structure.
|
||||
- Utilize umbrella projects to divide a large application into smaller, independent applications.
|
||||
- Consider code splitting at the LiveView level, using separate LiveView modules for different sections of a page.
|
||||
- Use dynamic imports for JavaScript modules to load code on demand.
|
||||
|
||||
- ## 2. Common Patterns and Anti-patterns
|
||||
|
||||
- ### 2.1. Design Patterns
|
||||
|
||||
- **MVC (Model-View-Controller)**: The core architectural pattern in Phoenix. Ensure proper separation of concerns between models (schemas), views (templates), and controllers.
|
||||
- **Context Pattern**: Encapsulate business logic and data access within contexts to provide a clear API for controllers and other parts of the application.
|
||||
- **Repository Pattern**: Abstract away database interactions behind a repository interface to allow for easier testing and potential database changes. While contexts often serve this purpose in Phoenix, a dedicated repository layer can be useful for very complex data access logic.
|
||||
- **PubSub**: Use `Phoenix.PubSub` for real-time communication between different parts of the application (e.g., LiveView components, channels).
|
||||
- **Ecto Changesets**: Employ Ecto changesets for data validation and sanitization before persisting to the database.
|
||||
|
||||
- ### 2.2. Recommended Approaches for Common Tasks
|
||||
|
||||
- **Authentication**: Use a library like `Pow` or `Ueberauth` for handling user authentication and authorization.
|
||||
- **Authorization**: Implement role-based access control (RBAC) or attribute-based access control (ABAC) using libraries like `Pomegranate` or custom logic.
|
||||
- **Form Handling**: Use `Phoenix.HTML.Form` helpers and Ecto changesets for building and validating forms.
|
||||
- **Real-time Updates**: Leverage Phoenix Channels or LiveView for real-time updates and interactive features.
|
||||
- **Background Jobs**: Use `Oban` for reliable background job processing.
|
||||
|
||||
- ### 2.3. Anti-patterns and Code Smells
|
||||
|
||||
- **Fat Controllers**: Avoid putting too much logic in controllers. Delegate to contexts or services.
|
||||
- **Direct Repo Access in Controllers**: Do not directly call `Repo` functions in controllers. Use contexts.
|
||||
- **Ignoring Changeset Errors**: Always handle changeset errors and display appropriate messages to the user.
|
||||
- **Over-Complicated Queries**: Keep Ecto queries simple and composable. Use views or functions in schema modules to build more complex queries.
|
||||
- **Unnecessary Global State**: Minimize the use of global state. Prefer passing data explicitly between functions and components.
|
||||
|
||||
- ### 2.4. State Management Best Practices (LiveView)
|
||||
|
||||
- Store only the minimal required state in LiveView's `assigns`.
|
||||
- Use `handle_params` to load data based on URL parameters.
|
||||
- Employ `handle_info` for handling asynchronous events and updates.
|
||||
- Implement proper state cleanup in `mount` and `terminate` callbacks.
|
||||
- Consider using a global state management library like `global` only when truly necessary for cross-component communication.
|
||||
|
||||
- ### 2.5. Error Handling Patterns
|
||||
|
||||
- Use pattern matching to handle different error scenarios.
|
||||
- Raise exceptions only for truly exceptional cases. For expected errors, return `{:error, reason}` tuples.
|
||||
- Implement error logging and monitoring using tools like `Sentry` or `Bugsnag`.
|
||||
- Use `try...rescue` blocks for handling potential exceptions during external API calls or file operations.
|
||||
- Define custom error types using atoms or structs to provide more context for error handling.
|
||||
|
||||
- ## 3. Performance Considerations
|
||||
|
||||
- ### 3.1. Optimization Techniques
|
||||
|
||||
- **Database Queries**: Optimize Ecto queries by using indexes, preloading associations, and avoiding N+1 queries.
|
||||
- **Caching**: Implement caching for frequently accessed data using `ETS` tables or a dedicated caching library like `Cachex`.
|
||||
- **Concurrency**: Leverage Elixir's concurrency features (e.g., `Task`, `GenServer`) to handle concurrent requests and background tasks efficiently.
|
||||
- **Code Profiling**: Use tools like `Erlang's observer` or `fprof` to identify performance bottlenecks in your code.
|
||||
- **Connection Pooling**: Ensure proper database connection pooling to minimize connection overhead.
|
||||
|
||||
- ### 3.2. Memory Management
|
||||
|
||||
- **Avoid Memory Leaks**: Be mindful of memory leaks, especially in long-running processes like `GenServers`.
|
||||
- **Use Streams**: Employ Elixir streams for processing large datasets in a memory-efficient manner.
|
||||
- **Garbage Collection**: Understand Erlang's garbage collection behavior and optimize code to minimize garbage collection overhead.
|
||||
|
||||
- ### 3.3. Rendering Optimization (LiveView)
|
||||
|
||||
- **Minimize DOM Updates**: Reduce the number of DOM updates in LiveView by using `phx-update` attributes and smart diffing.
|
||||
- **Use Static Content**: Serve static content (e.g., images, CSS, JavaScript) directly from the web server without involving LiveView.
|
||||
- **Optimize Templates**: Optimize HEEx templates for efficient rendering.
|
||||
|
||||
- ### 3.4. Bundle Size Optimization
|
||||
|
||||
- **Code Splitting**: Use code splitting to reduce the initial bundle size and load code on demand.
|
||||
- **Tree Shaking**: Enable tree shaking in esbuild or Webpack to remove unused code from the bundle.
|
||||
- **Minification**: Minify CSS and JavaScript files to reduce their size.
|
||||
- **Image Optimization**: Optimize images for web use by compressing them and using appropriate formats.
|
||||
|
||||
- ### 3.5. Lazy Loading
|
||||
|
||||
- **Lazy Load Images**: Implement lazy loading for images to improve initial page load time.
|
||||
- **Lazy Load Components**: Use dynamic imports or conditional rendering to lazy load LiveView components.
|
||||
|
||||
- ## 4. Security Best Practices
|
||||
|
||||
- ### 4.1. Common Vulnerabilities and Prevention
|
||||
|
||||
- **Cross-Site Scripting (XSS)**: Sanitize user input and use proper escaping in templates to prevent XSS attacks. Phoenix's HEEx templates automatically escape variables, but be careful when using `raw/1` or `safe/1`.
|
||||
- **Cross-Site Request Forgery (CSRF)**: Protect against CSRF attacks by using Phoenix's built-in CSRF protection mechanisms. Include the CSRF token in forms and AJAX requests.
|
||||
- **SQL Injection**: Use Ecto's parameterized queries to prevent SQL injection vulnerabilities.
|
||||
- **Authentication and Authorization Issues**: Implement robust authentication and authorization mechanisms to protect sensitive data and functionality.
|
||||
|
||||
- ### 4.2. Input Validation
|
||||
|
||||
- **Use Ecto Changesets**: Always validate user input using Ecto changesets to ensure data integrity and prevent malicious input.
|
||||
- **Whitelist Input**: Define a whitelist of allowed input values instead of a blacklist of disallowed values.
|
||||
- **Sanitize Input**: Sanitize user input to remove potentially harmful characters or code.
|
||||
|
||||
- ### 4.3. Authentication and Authorization
|
||||
|
||||
- **Use Strong Passwords**: Enforce strong password policies and use proper hashing algorithms (e.g., `bcrypt`) to store passwords securely.
|
||||
- **Implement Multi-Factor Authentication (MFA)**: Add an extra layer of security by requiring users to authenticate using multiple factors.
|
||||
- **Use JWT (JSON Web Tokens)**: Use JWT for secure API authentication and authorization.
|
||||
- **Implement Role-Based Access Control (RBAC)**: Define roles and permissions to control access to different parts of the application.
|
||||
|
||||
- ### 4.4. Data Protection
|
||||
|
||||
- **Encrypt Sensitive Data**: Encrypt sensitive data at rest and in transit using appropriate encryption algorithms.
|
||||
- **Use HTTPS**: Always use HTTPS to encrypt communication between the client and the server.
|
||||
- **Protect API Keys**: Store API keys securely and restrict access to them.
|
||||
|
||||
- ### 4.5. Secure API Communication
|
||||
|
||||
- **Use HTTPS**: Enforce HTTPS for all API communication.
|
||||
- **Validate API Requests**: Validate API requests to prevent malicious input and unauthorized access.
|
||||
- **Rate Limiting**: Implement rate limiting to prevent abuse and denial-of-service attacks.
|
||||
- **API Versioning**: Use API versioning to ensure backward compatibility and allow for future API changes.
|
||||
|
||||
- ## 5. Testing Approaches
|
||||
|
||||
- ### 5.1. Unit Testing
|
||||
|
||||
- **Test Contexts and Schemas**: Write unit tests for contexts and schemas to ensure that business logic and data validation work as expected.
|
||||
- **Mock External Dependencies**: Use mocking libraries like `Mock` or `Meck` to isolate units under test and avoid dependencies on external services.
|
||||
- **Test Edge Cases**: Test edge cases and boundary conditions to ensure that code handles unexpected input correctly.
|
||||
|
||||
- ### 5.2. Integration Testing
|
||||
|
||||
- **Test Controllers and Channels**: Write integration tests for controllers and channels to ensure that they interact correctly with contexts and other parts of the application.
|
||||
- **Use a Test Database**: Use a dedicated test database to avoid affecting the production database.
|
||||
- **Verify Database Interactions**: Verify that database interactions are performed correctly by inspecting the database state after running tests.
|
||||
|
||||
- ### 5.3. End-to-End Testing
|
||||
|
||||
- **Use Wallaby or Cypress**: Use end-to-end testing frameworks like Wallaby or Cypress to simulate user interactions and verify that the application works as expected from the user's perspective.
|
||||
- **Test Critical User Flows**: Test critical user flows to ensure that the most important features of the application are working correctly.
|
||||
|
||||
- ### 5.4. Test Organization
|
||||
|
||||
- **Organize Tests by Module**: Organize tests into directories that mirror the application's module structure.
|
||||
- **Use Descriptive Test Names**: Use descriptive test names to clearly indicate what each test is verifying.
|
||||
- **Keep Tests Small and Focused**: Keep tests small and focused to make them easier to understand and maintain.
|
||||
|
||||
- ### 5.5. Mocking and Stubbing
|
||||
|
||||
- **Use Mock Libraries**: Use mocking libraries like `Mock` or `Meck` to create mock objects and stub function calls.
|
||||
- **Avoid Over-Mocking**: Avoid over-mocking, as it can make tests less realistic and harder to maintain.
|
||||
- **Use Stubs for External Dependencies**: Use stubs to replace external dependencies with simplified versions that are easier to control during testing.
|
||||
|
||||
- ## 6. Common Pitfalls and Gotchas
|
||||
|
||||
- ### 6.1. Frequent Mistakes
|
||||
|
||||
- **Not Using Contexts**: Directly accessing the Repo in controllers or other modules, bypassing the business logic encapsulation provided by contexts.
|
||||
- **Ignoring Changeset Validation Errors**: Neglecting to handle and display changeset validation errors to the user, leading to confusing behavior.
|
||||
- **N+1 Queries**: Failing to preload associations in Ecto queries, resulting in performance bottlenecks.
|
||||
- **Over-reliance on Global State**: Using global state when it's not necessary, making code harder to reason about and test.
|
||||
|
||||
- ### 6.2. Edge Cases
|
||||
|
||||
- **Handling Timezones**: Dealing with timezones correctly, especially when storing and displaying dates and times to users in different locations.
|
||||
- **Concurrency Issues**: Avoiding race conditions and other concurrency issues when using Elixir's concurrency features.
|
||||
- **Large File Uploads**: Handling large file uploads efficiently and securely.
|
||||
|
||||
- ### 6.3. Version-Specific Issues
|
||||
|
||||
- **LiveView Updates**: Staying up-to-date with LiveView updates and understanding potential breaking changes.
|
||||
- **Ecto Version Compatibility**: Ensuring compatibility between Ecto and other dependencies.
|
||||
|
||||
- ### 6.4. Compatibility Concerns
|
||||
|
||||
- **JavaScript Framework Integration**: Integrating Phoenix with JavaScript frameworks like React or Vue.js can introduce compatibility issues.
|
||||
- **Database Driver Compatibility**: Ensuring compatibility between Ecto and the chosen database driver.
|
||||
|
||||
- ### 6.5. Debugging Strategies
|
||||
|
||||
- **Use IEx.pry**: Use `IEx.pry` to pause execution and inspect variables.
|
||||
- **Enable Debug Logging**: Enable debug logging to get more information about what's happening in the application.
|
||||
- **Use Remote Debugging**: Use remote debugging tools to debug applications running in production environments.
|
||||
- **Analyze Logs**: Analyze logs to identify errors and performance issues.
|
||||
|
||||
- ## 7. Tooling and Environment
|
||||
|
||||
- ### 7.1. Recommended Development Tools
|
||||
|
||||
- **Visual Studio Code**: VS Code with the ElixirLS extension provides excellent Elixir support, including code completion, linting, and debugging.
|
||||
- **IntelliJ IDEA**: IntelliJ IDEA with the Elixir plugin also provides excellent Elixir support.
|
||||
- **Mix**: Elixir's build tool, used for creating, compiling, testing, and managing dependencies.
|
||||
- **IEx**: Elixir's interactive shell, used for exploring code and debugging.
|
||||
- **Erlang Observer**: Erlang's GUI tool for monitoring and debugging Erlang/Elixir applications.
|
||||
|
||||
- ### 7.2. Build Configuration
|
||||
|
||||
- **Use `mix.exs`**: Use `mix.exs` to manage project dependencies, compilation settings, and other build configurations.
|
||||
- **Define Environments**: Define separate environments for development, testing, and production.
|
||||
- **Use Configuration Files**: Use configuration files (`config/config.exs`, `config/dev.exs`, `config/test.exs`, `config/prod.exs`) to configure the application for different environments.
|
||||
- **Secrets Management**: Use environment variables or dedicated secrets management tools to store sensitive information like API keys and database passwords.
|
||||
|
||||
- ### 7.3. Linting and Formatting
|
||||
|
||||
- **Use `mix format`**: Use `mix format` to automatically format Elixir code according to a consistent style.
|
||||
- **Use Credo**: Use Credo to analyze Elixir code for style and potential issues.
|
||||
- **Configure Editor**: Configure the editor to automatically format code on save.
|
||||
|
||||
- ### 7.4. Deployment
|
||||
|
||||
- **Use Mix Release**: Use `mix release` to build and deploy Elixir applications. Distillery is deprecated.
|
||||
- **Use Docker**: Use Docker to containerize Elixir applications for easy deployment and scaling.
|
||||
- **Deploy to Cloud Platforms**: Deploy Elixir applications to cloud platforms like Heroku, AWS, Google Cloud, or Azure.
|
||||
- **Use a Process Manager**: Use a process manager like `systemd` or `pm2` to manage Elixir application processes.
|
||||
|
||||
- ### 7.5. CI/CD Integration
|
||||
|
||||
- **Use GitHub Actions, GitLab CI, or CircleCI**: Use CI/CD platforms to automate the build, test, and deployment processes.
|
||||
- **Run Tests on CI**: Run tests on CI to ensure that code changes don't introduce regressions.
|
||||
- **Automate Deployments**: Automate deployments to production environments after successful tests.
|
||||
|
||||
- ## Additional Resources
|
||||
|
||||
- [Phoenix Framework Documentation](https://www.phoenixframework.org/docs)
|
||||
- [Elixir Language Documentation](https://elixir-lang.org/docs.html)
|
||||
- [Ecto Documentation](https://hexdocs.pm/ecto/Ecto.html)
|
||||
- [Phoenix LiveView Documentation](https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.html)
|
||||
- [Oban Documentation](https://github.com/sorentwo/oban)
|
||||
- [Credo Documentation](https://hexdocs.pm/credo/Credo.html)
|
||||
# phoenix Best Practices
|
||||
|
||||
This guide outlines the definitive best practices for developing Phoenix applications. Adhere to these rules to ensure consistency, maintainability, and high performance across our codebase.
|
||||
|
||||
## Code Organization and Structure
|
||||
|
||||
### 1. Context-Driven Design
|
||||
Encapsulate all data access and business logic for a feature within a dedicated context module. Controllers and LiveViews must remain thin, focusing solely on HTTP/WebSocket handling.
|
||||
|
||||
**✅ GOOD:**
|
||||
```elixir
|
||||
# lib/my_app/accounts.ex
|
||||
defmodule MyApp.Accounts do
|
||||
alias MyApp.Repo
|
||||
alias MyApp.Accounts.User
|
||||
|
||||
def create_user(attrs) do
|
||||
%User{}
|
||||
|> User.changeset(attrs)
|
||||
|> Repo.insert()
|
||||
end
|
||||
|
||||
def get_user!(id), do: Repo.get!(User, id)
|
||||
end
|
||||
|
||||
# lib/my_app_web/controllers/user_controller.ex
|
||||
defmodule MyAppWeb.UserController do
|
||||
use MyAppWeb, :controller
|
||||
alias MyApp.Accounts
|
||||
|
||||
def create(conn, %{"user" => user_params}) do
|
||||
case Accounts.create_user(user_params) do
|
||||
{:ok, user} ->
|
||||
conn
|
||||
|> put_flash(:info, "User created successfully.")
|
||||
|> redirect(to: ~p"/users/#{user}")
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
conn
|
||||
|> put_flash(:error, "Failed to create user.")
|
||||
|> render("new.html", changeset: changeset)
|
||||
end
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
### 2. Single Module Per Source File
|
||||
Each `.ex` file must contain a single module. This includes structs.
|
||||
|
||||
**❌ BAD:**
|
||||
```elixir
|
||||
# lib/payment/worker.ex
|
||||
defmodule Payment.Worker do
|
||||
defmodule WorkerConfiguration do
|
||||
defstruct [:retry_period_ms, :error_retry_limit]
|
||||
end
|
||||
# ...
|
||||
end
|
||||
```
|
||||
|
||||
**✅ GOOD:**
|
||||
```elixir
|
||||
# lib/payment/worker.ex
|
||||
defmodule Payment.Worker do
|
||||
# ...
|
||||
end
|
||||
|
||||
# lib/payment/worker_configuration.ex
|
||||
defmodule Payment.WorkerConfiguration do
|
||||
defstruct [:retry_period_ms, :error_retry_limit]
|
||||
end
|
||||
```
|
||||
|
||||
### 3. Naming Conventions
|
||||
Adhere strictly to Elixir's naming conventions.
|
||||
|
||||
* **Modules:** `PascalCase`
|
||||
* **Functions, Variables, Files, Directories:** `snake_case`
|
||||
|
||||
**❌ BAD:**
|
||||
```elixir
|
||||
defmodule Payment.jobs.Inquiry_scheduler do # Module, file, dir naming
|
||||
defp calculateBalance(%Entry{bookType: "debit", amount: amount}, balance) do # Function, variable naming
|
||||
balance - amount
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
**✅ GOOD:**
|
||||
```elixir
|
||||
# lib/payment/jobs/inquiry_scheduler.ex
|
||||
defmodule Payment.Jobs.InquiryScheduler do
|
||||
defp calculate_balance(%Entry{book_type: "debit", amount: amount}, balance) do
|
||||
balance - amount
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
### 4. Code Formatting
|
||||
Always run `mix format`. Configure `.formatter.exs` with `line_length: 100`. Use 2-space soft tabs. End every file with a newline.
|
||||
|
||||
## Common Patterns and Anti-patterns
|
||||
|
||||
### 1. Bang Functions (`!`) for Raising Errors
|
||||
Suffix functions that are expected to raise an exception (e.g., on invalid input or missing record) with `!`. This clearly signals to callers that the function might fail.
|
||||
|
||||
**✅ GOOD:**
|
||||
```elixir
|
||||
def create_user!(registration_uid, customer_params) do
|
||||
# ... logic that might raise ArgumentError or Ecto.NoResultsError
|
||||
end
|
||||
|
||||
defp build_attrs!(%{
|
||||
"alien_id" => alien_id,
|
||||
"citizen_id" => citizen_id,
|
||||
}) do
|
||||
%{citizen_id: citizen_id, alien_id: alien_id}
|
||||
end
|
||||
|
||||
defp build_attrs!(_params) do
|
||||
raise ArgumentError, message: "Unexpected customer profile params"
|
||||
end
|
||||
```
|
||||
|
||||
### 2. Prefer Pattern Matching and Guards
|
||||
Use pattern matching and guards in function definitions over `if/else` statements within the function body for clearer, more declarative logic.
|
||||
|
||||
**❌ BAD:**
|
||||
```elixir
|
||||
defp handle_response({:ok, %Response{status: status, body: body}}) do
|
||||
if status in 200..299 do
|
||||
{:ok, body}
|
||||
else
|
||||
{:error, :internal_server_error, reason}
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
**✅ GOOD:**
|
||||
```elixir
|
||||
defp handle_response({:ok, %Response{status: status, body: body}})
|
||||
when status in 200..299,
|
||||
do: {:ok, body}
|
||||
|
||||
defp handle_response({:error, reason}),
|
||||
do: {:error, :internal_server_error, reason}
|
||||
```
|
||||
|
||||
### 3. Pipe Operator Usage
|
||||
Avoid using the pipe operator for a single function call. Start a pipeline with the raw value. Use `Kernel.then/2` for anonymous functions in a pipeline.
|
||||
|
||||
**❌ BAD:**
|
||||
```elixir
|
||||
some_string |> String.downcase()
|
||||
value |> (&(&1 * 10)).()
|
||||
```
|
||||
|
||||
**✅ GOOD:**
|
||||
```elixir
|
||||
String.downcase(some_string)
|
||||
some_string |> String.trim() |> String.downcase() # Raw value start
|
||||
value |> then(&(&1 * 10)) # then/2 for anonymous functions
|
||||
```
|
||||
|
||||
### 4. Alias Grouping
|
||||
Use shorthand syntax to group aliases from the same submodule.
|
||||
|
||||
**❌ BAD:**
|
||||
```elixir
|
||||
alias Payment.Ledger.Balance
|
||||
alias Payment.Ledger.EntriesGenerator
|
||||
```
|
||||
|
||||
**✅ GOOD:**
|
||||
```elixir
|
||||
alias Payment.Ledger.{Balance, EntriesGenerator}
|
||||
```
|
||||
|
||||
### 5. Conditional Statements
|
||||
Use `do:` for single-line `if/unless`. Never use `unless` with `else`; rewrite with `if`.
|
||||
|
||||
**❌ BAD:**
|
||||
```elixir
|
||||
if some_condition do "some_stuff" end
|
||||
unless success do IO.puts("failure") else IO.puts("success") end
|
||||
```
|
||||
|
||||
**✅ GOOD:**
|
||||
```elixir
|
||||
if some_condition, do: "some_stuff"
|
||||
if success, do: IO.puts("success"), else: IO.puts("failure")
|
||||
```
|
||||
|
||||
### 6. Ignored Variables
|
||||
Use `_variable_name` for ignored variables to improve readability.
|
||||
|
||||
**❌ BAD:**
|
||||
```elixir
|
||||
def index(conn, _), do: Plug.Conn.resp(conn, :ok, "success")
|
||||
```
|
||||
|
||||
**✅ GOOD:**
|
||||
```elixir
|
||||
def index(conn, _params), do: Plug.Conn.resp(conn, :ok, "success")
|
||||
```
|
||||
|
||||
### 7. Private Functions
|
||||
Place all private functions at the bottom of the module.
|
||||
|
||||
## Performance Considerations
|
||||
|
||||
Phoenix leverages the Erlang VM for high concurrency and fault tolerance.
|
||||
* **Ecto Queries**: Optimize database queries. Use `Ecto.Query.preload/3` to avoid N+1 issues.
|
||||
* **LiveView**: Embrace LiveView for interactive UIs to minimize client-side JavaScript and network roundtrips.
|
||||
* **Concurrency**: Design long-running tasks to run in separate processes (e.g., using `Task` or `GenServer`) to avoid blocking the request cycle.
|
||||
|
||||
## Common Pitfalls and Gotchas
|
||||
|
||||
### 1. Scattering Business Logic
|
||||
The most common pitfall is scattering business logic across controllers, LiveViews, and schemas. Always centralize it in contexts.
|
||||
|
||||
### 2. Over-reliance on Metaprogramming
|
||||
While powerful, excessive `use` or `import` can obscure where functions originate. Rely on documentation and `h/1` in IEx to understand module APIs.
|
||||
|
||||
### 3. Deployment Complexity
|
||||
Elixir Releases with Docker simplify deployment, but still require careful configuration of environment variables and build steps (e.g., `npm install` for assets). Use `mix release` and the generated `Dockerfile`.
|
||||
|
||||
## Security Best Practices
|
||||
|
||||
### 1. Authentication
|
||||
Use `mix phx.gen.auth` for a robust, production-ready authentication system.
|
||||
|
||||
### 2. Input Validation
|
||||
Always validate user input using Ecto Changesets. Never trust client-side data.
|
||||
|
||||
**✅ GOOD:**
|
||||
```elixir
|
||||
def changeset(user, attrs) do
|
||||
user
|
||||
|> cast(attrs, [:email, :password, :password_confirmation])
|
||||
|> validate_required([:email, :password])
|
||||
|> validate_length(:password, min: 8)
|
||||
|> unique_constraint(:email)
|
||||
|> put_assoc(:profile, profile_changeset(user.profile, attrs))
|
||||
end
|
||||
```
|
||||
|
||||
### 3. XSS and CSRF Protection
|
||||
Phoenix provides built-in protection against XSS (via HTML escaping in HEEx templates) and CSRF (via `Plug.CSRFProtection`). Ensure these are active.
|
||||
|
||||
## Error Handling
|
||||
|
||||
### 1. Return Tuples for Expected Outcomes
|
||||
For operations that can succeed or fail, consistently return `{:ok, result}` or `{:error, reason}` tuples.
|
||||
|
||||
**✅ GOOD:**
|
||||
```elixir
|
||||
def update_user(user, attrs) do
|
||||
user
|
||||
|> User.changeset(attrs)
|
||||
|> Repo.update()
|
||||
end
|
||||
```
|
||||
|
||||
### 2. Centralized Logging and Error Tracking
|
||||
Integrate with a centralized logging solution (e.g., Graylog via `GelfLogger`) and an error tracking service (e.g., Sentry) for production monitoring.
|
||||
|
||||
## API Design
|
||||
|
||||
### 1. Contexts as API Boundaries
|
||||
Design your contexts to expose a clear, stable API for interacting with your application's data and business logic.
|
||||
|
||||
### 2. RESTful JSON APIs
|
||||
Use `mix phx.gen.json` to scaffold RESTful JSON APIs. Ensure consistent response formats and HTTP status codes.
|
||||
|
||||
## Testing Approaches
|
||||
|
||||
### 1. Comprehensive ExUnit Testing
|
||||
Write tests for all layers: contexts, controllers, LiveViews, and channels. Run tests with `mix test`.
|
||||
|
||||
### 2. Leverage Generated Test Cases
|
||||
Phoenix generators (`mix phx.gen.live`, `mix phx.gen.html`, etc.) provide excellent examples for testing each component.
|
||||
|
||||
### 3. `ConnCase` and `DataCase`
|
||||
Use `HelloWeb.ConnCase` for controller/LiveView tests (provides `conn` and web helpers) and `Hello.DataCase` for context/Ecto tests (manages database sandbox).
|
||||
|
||||
**✅ GOOD:**
|
||||
```elixir
|
||||
# test/my_app_web/controllers/user_controller_test.exs
|
||||
defmodule MyAppWeb.UserControllerTest do
|
||||
use MyAppWeb.ConnCase, async: true
|
||||
alias MyApp.Accounts
|
||||
|
||||
setup [:create_user]
|
||||
|
||||
defp create_user(_) do
|
||||
{:ok, user} = Accounts.create_user(%{email: "test@example.com", password: "password"})
|
||||
%{user: user}
|
||||
end
|
||||
|
||||
test "shows existing user", %{conn: conn, user: user} do
|
||||
conn = get(conn, ~p"/users/#{user}")
|
||||
assert html_response(conn, 200) =~ user.email
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
### 4. LiveView Testing Helpers
|
||||
For LiveView tests, use `render_hook/2`, `render_component/2`, `form/2`, `assert_patch/2`, etc. When debugging HTML output, use specific element selectors rather than broad string matching.
|
||||
|
||||
**✅ GOOD (using custom helpers for clarity):**
|
||||
```elixir
|
||||
# test/support/element_helpers.ex
|
||||
defmodule MyApp.Support.ElementHelpers do
|
||||
import Phoenix.LiveViewTest
|
||||
|
||||
def get_by_role(view, :button, text_filter) do
|
||||
button = view |> element("button", text_filter)
|
||||
assert has_element?(button)
|
||||
button
|
||||
end
|
||||
|
||||
def get_by_role(view, :textbox, text_filter) do
|
||||
label = view |> element("label", text_filter)
|
||||
textbox_id = Regex.run(~r/(?<=for=")\w+(?=")/, label |> render) |> Enum.at(0)
|
||||
input = view |> element("input##{textbox_id}")
|
||||
textbox = if has_element?(input), do: input, else: view |> element("textarea##{textbox_id}")
|
||||
assert has_element?(label)
|
||||
assert has_element?(textbox)
|
||||
textbox
|
||||
end
|
||||
end
|
||||
|
||||
# test/my_app_web/live/user_live_test.exs
|
||||
defmodule MyAppWeb.UserLiveTest do
|
||||
use MyAppWeb.ConnCase, async: true
|
||||
import Phoenix.LiveViewTest
|
||||
import MyApp.Support.ElementHelpers # Import your custom helpers
|
||||
|
||||
test "creates a new user", %{conn: conn} do
|
||||
{:ok, lv, _html} = live(conn, ~p"/users/new")
|
||||
|
||||
lv
|
||||
|> get_by_role(:textbox, "Email")
|
||||
|> render_change(value: "new@example.com")
|
||||
|
||||
lv
|
||||
|> get_by_role(:textbox, "Password")
|
||||
|> render_change(value: "securepassword")
|
||||
|
||||
lv
|
||||
|> get_by_role(:button, "Create User")
|
||||
|> render_click()
|
||||
|
||||
assert_patch(lv, ~p"/users")
|
||||
assert has_element?(lv.html, "User created successfully.")
|
||||
end
|
||||
end
|
||||
```
|
||||
Reference in New Issue
Block a user