Appearance
Design-related anti-patterns
Source: Elixir guide, Design-related anti-patterns.
These anti-patterns are about modules, functions, and the role that they play. There are six.
Alternative return types
Problem: an options argument (usually a Keyword listA list of tuples with two elements, where the first element, the key, is an atom. The keys keep the order that you gave and can repeat. People mostly use them for optional arguments to functions.Read the page · Glossary) changes the return type in a big way. You cannot tell what the function returns unless you know the options.
elixir
AlternativeInteger.parse("13") #=> {13, ""}
AlternativeInteger.parse("13", discard_rest: true) #=> 13Refactoring: use one function for each return type: parse/1 and parse_discard_rest/1.
Boolean obsession
Problem: several booleans with overlapping meaning encode a state that AtomA constant with a value that is its own name, such as :ok. Two atoms are equal if their names are equal.Read the page · Glossary express better. With admin: true and editor: true, :editor has no effect if :admin is set.
elixir
cond do
options[:admin] -> …
options[:editor] -> …
true -> …
endRefactoring: use one :role option with the values :admin, :editor, or :default. The same is true for StructAn extension that is built on top of maps. It adds compile-time checks and default values.Read the page · Glossary fields. Even a single boolean can be an atom (status: :approved) that leaves room for a :pending state later. There is no cost in performance, because booleans are atoms.
Exceptions for control flow
Problem: try/rescue decides what happens next, when a case on a returned 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 is enough. Authors of libraries must let callers decide if an error is exceptional.
elixir
try do
IO.puts(File.read!(file))
rescue
e -> IO.puts(:stderr, Exception.message(e))
endRefactoring: use the File.read/1 function that does not raise errors, and match on its result:
elixir
case File.read(file) do
{:ok, binary} -> IO.puts(binary)
{:error, reason} -> IO.puts(:stderr, "could not read file #{file}: #{reason}")
endLibraries must offer both variants, with the bang version built on the plain version. By convention, the plain function returns {:ok, result} or {:error, Exception.t}, so the caller can raise it.
It is acceptable to raise errors for invalid arguments (File.read(123)) and in tests and scripts. It is also acceptable in frameworks such as Phoenix that turn exceptions into HTTP responses.
Primitive obsession
Problem: basic types (strings, integers, floats) carry structured information. If an address is one string, you must parse it again and again. Floats for money are another example (use a richer type).
Refactoring: parse once into a struct and pass the struct around:
elixir
defmodule Address do
defstruct [:street, :city, :state, :postal_code, :country]
end
def extract_postal_code(%Address{} = address), do: …Unrelated multi-clause function
Problem: the clauses of one function do completely different things, and the @doc fills with conditionals.
elixir
def update(%Product{count: count, material: material}), do: …
def update(%Animal{count: count, skin: skin}), do: …Refactoring: split the function into update_product/1 and update_animal/1 (or separate modules), each with its own @doc. A function can still have several clauses if they group related behavior. The +/2 function of Elixir handles integers and floats together, but strings use <>/2. struct/2 works for any struct because it behaves in the same way for all of them.
Using application configuration for libraries
Problem: the application environment is global. A library that reads its behavior from it lets each key have only one value. Two applications that depend on the same library cannot configure it in different ways.
elixir
parts = Application.fetch_env!(:app_config, :parts)
String.split(string, "-", parts: parts)Refactoring: take options on the function: split(string, opts \\ []), with defaults.
Not every use is wrong. One example is to swap a component that must behave in an identical way (a CSV parser), ideally with a behaviour that fixes its semantics.
- Supervision trees: the library provides a Child specificationA map that tells a supervisor how to start, shut down, and restart one child. You usually write it as
{module, options}or only the module name.Read the page · Glossary and does not start its own tree. You list the spec in your tree:{DNSCluster, query: "my.subdomain"}. You can read the environment yourself. - Compile-time configuration: let users generate the code:
use Ecto.Repo, adapter: …, so that you can define as many repos as you want. - Mix tasks: read the configuration of each project from
Mix.Project.config/0(a key inproject/0) or from command-line options withOptionParser.