Skip to main content

Inappropriate coding practices - Unnecessary imports

Need

To maintain optimal application performance and readability by only importing necessary modules.

Context

  • Usage of Elixir (1.12.0 and above) for building scalable and fault-tolerant applications

Description

Non compliant code

defmodule MyApp do
import Enum
import List
def hello do
IO.puts('Hello, world!')
end
end

In this insecure code example, the modules Enum and List are being imported but not used in any functions. This results in unnecessary load and can cause suboptimal performance.

Steps

  • Review the code and identify the imported modules.
  • Identify which modules are actually being used by the functions in your code.
  • Remove the import statements for the unused modules.

Compliant code

defmodule MyApp do
def hello do
IO.puts('Hello, world!')
end
end

In this secure code example, no modules are being imported because they are not needed for the function hello. This minimizes load and optimizes performance.

References