Erlang idioms and clean code

Immutable data

Every value in Erlang is fixed at creation. Code that needs a changed list builds a new one and passes it along.

This removes whole families of bugs: no stale copy, no surprise alias, no lock around shared mutable state. This module follows the idioms of the lesson; save it as users.erl.

-module(users). -export([fetch/1, is_adult/1, prepend/2, name/1]). fetch(Id) when Id > 0 -> {ok, #{id => Id, name => "Ada"}}; fetch(_Id) -> {error, not_found}. is_adult(#{age := Age}) -> Age > 18. prepend(Item, List) -> [Item | List]. name(User) -> maps:get(name, User).

erlc users.erl
erl

1> Old = [2, 3], New = users:prepend(1, Old), {Old, New}.
{[2,3],[1,2,3]}

Small functions

A function does one thing and names it. Five lines is a good size, and twenty is a smell.

Small functions give the pattern matcher small pieces to work with, and the compiler small units to warn about.

erl

1> users:is_adult(#{age => 20}).
true
2> users:is_adult(#{age => 12}).
false

Let it crash

Do not wrap every call in defensive checks. Let the error surface where it happens, and let the supervisor restart the broken part.

The rule sounds reckless and reads clearly: crash loudly in a small scope, recover in a big one. A match is the cheapest check there is.

erl

1> {ok, User} = users:fetch(1).
{ok,#{id => 1,name => "Ada"}}
2> {ok, Missing} = users:fetch(-1).
** exception error: no match of right hand side value {error,not_found}

Naming rules

Variables start uppercase, functions and atoms start lowercase, and modules are all lowercase with underscores. Records and their fields are lowercase too.

The casing is not decoration: it tells the reader what kind of thing every name is, at a glance.

erl

1> UserName = users:name(#{name => "Ada"}).
"Ada"
2> lists:reverse(UserName).
"adA"

Next: Rebar3 builds

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What happens to a list when you prepend an item?

    Answer

    A new list is built and the old one stays as it was

    Values never change: [Item | List] builds a new list and leaves the original bound value alone.

  2. What does {Old, New} prove in the prepend example?

    Answer

    Old is unchanged and New starts with the new item

    The old list is still [2,3], and only the new binding carries the 1 at the front.

  3. How long should a function be, according to the lesson?

    Answer

    Five lines is a good size

    A small function does one thing, and the pattern matcher and the compiler work best on small units.

  4. What does is_adult(#{age := Age}) -> Age > 18. match?

    Answer

    A map that has the key age

    The map pattern #{age := Age} requires the key and binds its value; a map without age does not match.

  5. What does users:is_adult(#{age => 12}) answer?

    Answer

    false

    12 is not greater than 18, so the comparison answers false.

  6. What does "let it crash" mean?

    Answer

    Crash in a small scope and let a supervisor recover the big one

    The failing process dies with a clear reason, and the supervision tree restarts it.

  7. What happens at {ok, Missing} = users:fetch(-1).?

    Answer

    The match fails with a badmatch exception

    The right side is {error,not_found}, which does not match {ok, Missing}, so the match raises.

  8. Which name is a variable?

    Answer

    UserName

    Variables start with an uppercase letter; lowercase names are atoms or functions.

  9. Which name is a module?

    Answer

    users

    Modules are all lowercase with underscores, and the file name must match.

  10. Why is immutability a gift for concurrent code?

    Answer

    No shared value can change under another process

    With no shared mutable state there is no lock to take and no stale copy to guard against.

The examples on this page were compiled and run with Erlang/OTP 27 (erts-15.2.7.14) on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev