LFE Programming Rules and Conventions

2 Structure and LFE Terminology

Erlang/OTP, and thus LFE/OTP, systems are divided into modules. Modules are composed of functions and attributes. Functions are either only visible inside a module or they are exported i.e. they can also be called by other functions in other modules.

Attributes in LFE are written the same as functions, and should be placed at the beginning of a module or in a dedicated constants module. As they are fucntions, when used in code, they are called.

The work in a system designed using LFE is done by processes. A process is a job which can use functions in many modules. Processes communicate with each other by sending messages. Processes receive messages which are sent to them and a process can decide which messages it is prepared to receive. Other messages are queued until the receiving process is prepared to receive them.

A process can supervise the existence of another process by setting up a link to it. When a process terminates, it automatically sends exit signals to the process to which it is linked. The default behavior of a process receiving an exit signal is to terminate and to propagate the signal to its linked processes. A process can change this default behavior by trapping exits, this causes all exit signals sent to a process to be turned into messages.

A pure function is a function that returns the same value given the same arguments regardless of the context of the call of the function. This is what we normally expect from a mathematical function. A function that is not pure is said to have side effects.

Side effects typically occur if a function

  1. sends a message
  2. receives a message
  3. calls exit
  4. calls any BIF which changes a processes environment or mode of operation (e.g. get/1, put/2, erase/1, process_flag/2, etc)..