When you build software in Erlang (including Elixir) one good practice is to use what can be called “Tuple Types”. This enables you to get code to the state of correct by construction.

So for example if you have a user id that is a GUID such as “c1fe8fba-f0c9-45f0-aef8-7a7ebfaa6277”, then instead of having the user id just be that guid, use a tagged tuple like this {'user_id', <<"c1fe8fba-f0c9-45f0-aef8-7a7ebfaa6277">>}.

This has several advantages for developers. imagine the following code (and imagine it does something useful).

-type user_id() :: {user_id, binary()}.

-spec(do_something_with_user(user_id()) -> ok).
do_something_with_user(U = {user_id, _}) -> ok.

There are a few things that we get from this. First of all dialyzer can now find any errors. if we try to pass something that is not a user_id() it will be caught by dialyzer. This should prevent all sorts of bugs from entering your code.

Secondly if you try to pass some other type such as {store_id, "..."} it will give a pattern match error so you have protection from bad data being passed into the system.

Finally if you pass the data into a log file it becomes obvious what it is. If you see b03d69a1-cfb4-4c7a-a4ef-88ca866f5015 in a logfile it could be anything, but if you see {user_id, "b03d69a1-cfb4-4c7a-a4ef-88ca866f5015"} in your log file it is obvious to you that this is a user id.

This is a simple thing but it will help you write better code in Erlang and Elixir.

I will be giving a 4-day learning Elixir class in London