- Introduction
- Design-related smells
- GenServer Envy
- Agent Obsession
- Unsupervised process
- Large messages
- Unrelated multi-clause function
- Complex extractions in clauses 1
- Using exceptions for control-flow
- Untested polymorphic behaviors
- Code organization by process
- Large code generation by macros 1
- Data manipulation by migration
- Using App Configuration for libraries
- Compile-time global configuration
- "Use" instead of "import"
- Low-level concerns smells
- Traditional code smells
- About
- Acknowledgments
Elixir is a functional programming language whose popularity is rising in the industry link. However, there are few works in the scientific literature focused on studying the internal quality of systems implemented in this language.
In order to better understand the types of sub-optimal code structures that can harm the internal quality of Elixir systems, we scoured websites, blogs, forums, and videos (grey literature review), looking for specific code smells for Elixir that are discussed by its developers.
As a result of this investigation, we have initially proposed a catalog of 18 new smells that are specific to Elixir systems. After that, 1 new smell emerged from a study with mining software repositories (MSR) performed by us, and other smells are being suggested by the community, so this catalog is constantly being updated (currently 23 smells). These code smells are categorized into two different groups (design-related and low-level concerns), according to the type of impact and code extent they affect. This catalog of Elixir-specific code smells is presented below. Each code smell is documented using the following structure:
-
Name: Unique identifier of the code smell. This name is important to facilitate communication between developers;
-
Category: The portion of code affected by smell and its severity;
-
Problem: How the code smell can harm code quality and what impacts this can have for developers;
-
Example: Code and textual descriptions to illustrate the occurrence of the code smell;
-
Refactoring: Examples of refactored code are presented to illustrate higher-quality alternatives compared to smelly code;
-
Treatments: How to remove a code smell in a disciplined way with the assistance of refactoring strategies. When a refactoring should be used alone, it is listed in its own bullet point (i.e., •). Conversely, when a refactoring is part of a sequence of operations to assist the removal, it is listed using a pipeline to define its order (e.g., Refactoring1 |> Refactoring2 |> Refactoring3 |> etc.). This disciplined way to refactor a smell will help you change your code one small step at a time, thus minimizing the chances of introducing bugs or altering the original behavior of the system. All the refactoring strategies mapped to the code smells are part of our Catalog of Elixir Refactorings.
In addition to the Elixir-specific code smells, our catalog also documents 12 traditional code smells discussed in the context of Elixir systems.
The objective of this catalog of code smells is to instigate the improvement of the quality of code developed in Elixir. For this reason, we are interested in knowing Elixir's community opinion about these code smells: Do you agree that these code smells can be harmful? Have you seen any of them in production code? Do you have any suggestions about some Elixir-specific code smell not cataloged by us?...
Please feel free to make pull requests and suggestions (Issues tab). We want to hear from you!
Design-related smells are more complex, affect a coarse-grained code element, and are therefore harder to detect. In this section, 14 different smells classified as design-related are explained and exemplified:
-
Category: Design-related smell.
-
Problem: In Elixir, processes can be primitively created by
Kernel.spawn/1,Kernel.spawn/3,Kernel.spawn_link/1andKernel.spawn_link/3functions. Although it is possible to create them this way, it is more common to use abstractions (e.g.,Agent,Task, andGenServer) provided by Elixir to create processes. The use of each specific abstraction is not a code smell in itself; however, there can be trouble when either aTaskorAgentis used beyond its suggested purposes, being treated like aGenServer. -
Example: As shown next,
AgentandTaskare abstractions to create processes with specialized purposes. In contrast,GenServeris a more generic abstraction used to create processes for many different purposes:Agent: As Elixir works on the principle of immutability, by default no value is shared between multiple places of code, enabling read and write as in a global variable. AnAgentis a simple process abstraction focused on solving this limitation, enabling processes to share state.Task: This process abstraction is used when we only need to execute some specific action asynchronously, often in an isolated way, without communication with other processes.GenServer: This is the most generic process abstraction. The main benefit of this abstraction is explicitly segregating the server and the client roles, thus providing a better API for the organization of processes communication. Besides that, aGenServercan also encapsulate state (like anAgent), provide sync and async calls (like aTask), and more.
Examples of this code smell appear when
AgentsorTasksare used for general purposes and not only for specialized ones such as their documentation suggests. To illustrate some smell occurrences, we will cite two specific situations. 1) When aTaskis used not only to async execute an action, but also to frequently exchange messages with other processes; 2) When anAgent, beside sharing some global value between processes, is also frequently used to execute isolated tasks that are not of interest to other processes. -
Refactoring: When an
AgentorTaskgoes beyond its suggested use cases and becomes painful, it is better to refactor it into aGenServer. -
Treatments:
-
Category: Design-related smell.
-
Problem: In Elixir, an
Agentis a process abstraction focused on sharing information between processes by means of message passing. It is a simple wrapper around shared information, thus facilitating its read and update from any place in the code. The use of anAgentto share information is not a code smell in itself; however, when the responsibility for interacting directly with anAgentis spread across the entire system, this can be problematic. This bad practice can increase the difficulty of code maintenance and make the code more prone to bugs. -
Example: The following code seeks to illustrate this smell. The responsibility for interacting directly with the
Agentis spread across four different modules (i.e,A,B,C, andD).defmodule A do #... def update(pid) do #... Agent.update(pid, fn _list -> 123 end) #... end end
defmodule B do #... def update(pid) do #... Agent.update(pid, fn content -> %{a: content} end) #... end end
defmodule C do #... def update(pid) do #... Agent.update(pid, fn content -> [:atom_value | [content]] end) #... end end
defmodule D do #... def get(pid) do #... Agent.get(pid, fn content -> content end) #... end end
This spreading of responsibility can generate duplicated code and make code maintenance more difficult. Also, due to the lack of control over the format of the shared data, complex composed data can be shared. This freedom to use any format of data is dangerous and can induce developers to introduce bugs.
# start an agent with initial state of an empty list iex(1)> {:ok, agent} = Agent.start_link fn -> [] end {:ok, #PID<0.135.0>}