Basics
This check is disabled by default.
Learn how to enable it via .credo.exs.
This check has a base priority of normal and works with any version of Elixir.
Explanation
Comparing length/1 against an integer literal walks the whole list just
to answer a question about its size.
Unlike LengthInGuard, this fires in any context — guard or body — so
moving the comparison out of a guard into an if does not hide the
problem, it just relocates it.
# bad
if length(list) == 0, do: :empty
def page(list) when length(list) > 0, do: ...
length(items) <= 5
# good — emptiness
if list == [], do: :empty # or Enum.empty?(list)
def page([_ | _]), do: ...
# good — exact/maximum size
case list do
[_, _] -> ...
end
# good — threshold on a large or lazy enum
Enum.count_until(items, 6) <= 5length/1 itself is fine when you genuinely need the count (logging,
arithmetic, a value you reuse) — only the comparison against a literal is
flagged.
Check-Specific Parameters
There are no specific parameters for this check.
General Parameters
Like with all checks, general params can be applied.
Parameters can be configured via the .credo.exs config file.