LFE Style Guide
7 Language Usage Guidelines
7.1 Functional Style
Erlang is a functional language; LFE should be treated as such as well. You should not be modifying local variables, mutating data, etc.
If you use closures or processes to create stateful applications, be sure that functions which operate on these objects or processes return new data. Don't use setter functions that mutate state.
Writing code that is as purely functional as possible has many benefits, some of which are:
- It is safer to run that code across distributed nodes.
- It is easier to debug.
- It is easier to test.
You should take advantage of LFE's (Erlang's and OTP's) rich libraries and open source ecosystem to create programs in a functional style. Part of this includes taking advantage of recursion.
There are several idioms for the creation of recursive functions in LFE:
- Recursion via list and binary comprehensions.
- Recursion via
(cons ...), deconstructing lists in function heads.
- Tail-call optimised code.
- Naïve recursion (code that is not tail-call optimised).
To be clear, naïve recursion should not ever be used except for demonstration purposes. Erlang style typically promotes preferring comprehensions where applicable. In LFE, comprehensions are a bit less legible, so we leave the choice to the coder as a matter of preference.
If you do rely on proper tail calls, you should prominently document the fact, as tail calls tend to be less clear than naïve recursion.
7.3 Objects and State
7.4 Error Handling
For recommendations on error handling, see the Error Handling chapter of the LFE Programming Rules and Conventions Guide.
7.5 Processes, Servers and Messages
For recommendations on working with processes, servers and messages, see the Processes, Servers and Messages chapter of the LFE Programming Rules and Conventions Guide.
7.6 Various LFE Specific Conventions
See the Various LFE Specific Conventions chapter of the LFE Programming Rules and Conventions Guide for additional style guidance for LFE.
7.7 Specific Lexical and Stylistic Conventions
See the Specific Lexical and Stylistic Conventions chapter of the LFE Programming Rules and Conventions Guide for additional style guidance for LFE.
7.8 Documenting Code
For guidance on documenting code, see the following resources: