Appearance
Naming conventions
Source: Elixir reference, Naming conventions.
This page covers casing and punctuation. A convention is a subset of the syntax. It sets best practice for the language and the community. For the full syntax, see the Syntax reference.
Casing
- Use
snake_casefor variables, function names, module attributes, and similar names. - Use
CamelCasefor aliases (module names), and keep acronyms capitalized:OptionParser,ExUnit.CaptureIO,Mix.SCM. - Atoms can be
:snake_caseor:CamelCase, butsnake_caseis the convention. - File names follow the
snake_caseof the module:MyApplives inmy_app.ex. This is only a convention, because file names do not affect compiled code.
Underscore (_foo)
Send a value that you do not use to _ or to a variable that starts with _: {:ok, _contents} = File.read("README.md").
By default, Elixir never imports functions that start with _. Elixir uses them for compile-time metadata, usually in the __foo__ format. Every module has __info__/1, and there are the special forms __CALLER__/0, __DIR__/0, __ENV__/0, __MODULE__/0, and __STACKTRACE__/0.
Trailing bang (foo!)
This is a function or macro with failure cases that raise errors. It usually pairs with a version that returns :ok and :error TupleA fixed collection with curly brackets that can hold any value. Its elements are stored one after the other in memory, so it is fast to read one element by index or to get the size.Read the page · Glossary (or nil). File.read/1 returns {:ok, _} or {:error, :enoent}, and File.read!/1 returns the plain value or raises File.Error.
- Use the plain version when you want to handle outcomes with Pattern matchingThe
=operator is the match operator. It matches simple values, and it also destructures complex data such as tuples and binds variables to the parts.Read the page · Glossary. - Use the bang version when you expect success (the file must exist). It raises a more helpful error than a failed pattern match.
Errors from invalid argument types always raise an error, with or without a bang (File.read(123) raises FunctionClauseError). More pairs are Base.decode16/2 and decode16!/2, and File.cwd/0 and cwd!/0. Some bang functions have no plain counterpart. This leaves room for one later (Protocol.assert_protocol!/1).
Trailing question mark and is_
foo?is for functions that return a boolean:Keyword.keyword?/1,Mix.debug?/0,String.contains?/2.is_foois for type and boolean checks that you can use in guards, as in the Erlang convention:is_list/1,Integer.is_even/1. Checks that are not valid in guards do not use it (Keyword.keyword?/1).- Never use
?andis_together.
Special names
length and size
size means constant time (the size is stored with the data): map_size/1, tuple_size/1. length means linear time (the function traverses the whole structure): length/1, String.length/1.
get, fetch, fetch!
These apply to key-value data structures:
getreturns the value. When the key is missing, it returns a default (nilunless you give one).fetchreturns{:ok, value}. When the key is missing, it returns:error.fetch!returns the value. When the key is missing, it raises an error.
Examples: Map.get/2, Map.fetch/2, Map.fetch!/2, and the same functions in Keyword.
compare
compare/2 returns :lt, :eq, or :gt. Enum.sort/2 expects exactly this convention (DateTime.compare/2 is an example).