Introduction
Now that the language is picking up support and maturing nicely, I’d like to start a collection of common and recurring Elixir language idioms - recurring expressions or constructs in Elixir code that are not built-in features of the language.
What is a Language Idiom?
Consider this: in English one would usually say "the small black cat"
, even though grammar rules definitely deem "the black small cat"
a fully valid language construct.
When studying a language you may be good at learning the vocabulary (syntax) and catching on with the grammar, but you may fail to properly express yourself in that language. For instance, many English speakers learning Polish struggle trying to use English thoughts and then translating them to Polish. Even though they know all the right translations of those words, and the rules in which to order them, the end result is incomprehensible. Polish is a highly idiomatic language.
In a similar fashion, programming languages tend to have idioms - commonly used patterns that form an implicit agreement of the community on how to describe certain expressions/task/algorithms in a particular programming language.
Language idioms are commonly an indicator of the flexibility of a language - the more flexible a language is and the less rigid the rules are, the more equivalent ways tend to exist in coding tasks, algorithms and data structures that achieve the same result. For instance, languages like Perl, or Scala are sometimes called idiomatic languages - there exist dozens of idioms within these languages, whereas a language like Java does not seem to have many idioms.
Like many functional languages, Elixir has a surprising level of expressive power, allowing for high levels of succinctness in the programs we write. At the same time, it makes learning the language harder for new-comers as Elixir forces us to adopt Elixir’s way of expressing our thoughts and ideas in code.
Why should I care?
There are several benefits in establishing language idioms within a community, and being familiar with language idioms as learners of the language.
-
When reading code that someone else wrote, recognizing several lines of code as an idioms allows the reader to mentally file those lines into a particular corner (“loop iterating over all elements and calculating a sum!”). This allows us to reason and understand code on a more abstract level, without having to carefully read and comprehend each single line of code individually.
-
When writing code on your own, you won’t have to re-invent the wheel. Familiarity with Elixir idioms will allow you to be productive quickly without having to put in too much thought on how to achieve the same re-curring tasks over and over again. (“Uhm, how did I sort that Map last time by key-order?”).
Elixir Language Idiom Collection
Function calls return Tuples indicating Success/Error
- Functions return tuples based on success status
- The first element of this tuple is usually
:ok
on success, and:error
on (any kind of) failure. - Callers pattern-match against the success and error cases
Example
defmodule HelloPhoenix.RoomChannel do
use Phoenix.Channel
def join("room:lobby", _message, socket) do
{:ok, socket}
end
def join("room:" <> _private_room_id, _params, _socket) do
{:error, %{reason: "unauthorized"}}
end
end
Other Languages with this idiom
Python, Swift
What other Elixir idioms did you observe in your own code, or in others’ Elixir code so far?