Elixir Style Guide

From Elixir Wiki
Jump to navigation Jump to search

Elixir Style Guide[edit]

This style guide provides guidelines for writing clean and maintainable Elixir code. Adhering to these guidelines will not only improve the readability of your code but also promote consistency across projects. Following a consistent style in your Elixir codebase will make it easier for other developers to understand and contribute to your project.

Naming Conventions[edit]

- Use descriptive and meaningful names for variables, functions, and modules - Use snake_case for atoms and functions - Use CamelCase for modules and structs

Formatting[edit]

- Indent using 2 spaces - Use braces to wrap multiline function bodies - Place closing braces on the same line as the opening braces - Use a single-line comment (`#`) for comments that fit in one line - Use a multi-line comment (`"""`) for comments spanning multiple lines

Modules and Functions[edit]

- Use module attributes (`@`) to document and provide metadata - Define functions in a consistent order: public functions before private functions - Use pattern matching to define multiple function clauses

Error Handling[edit]

- Use `with` statements to handle multiple potential errors - Make use of the `{:ok, result}` and `{:error, reason}` tuple convention for function returns

Concurrency[edit]

- Opt for higher-level constructs like GenServer and Task when dealing with concurrency - Use message passing and pattern matching for communication between processes - Leverage supervisors for fault tolerance and graceful error handling

Testing[edit]

- Write comprehensive tests to verify code functionality - Use the ExUnit framework for writing unit tests - Organize tests into test cases and test suites

Documentation[edit]

- Provide meaningful and concise documentation for modules, functions, and types - Use Markdown syntax for formatting documentation

Resources[edit]

See Also[edit]

References[edit]