OeditusCredo.Check.Refactoring.PreferInplaceListMatching (OeditusCredo v0.10.2)

View Source

Basics

This check is disabled by default.

Learn how to enable it via .credo.exs.

This check has a base priority of high and works with any version of Elixir.

Explanation

Calling length/1 in guards forces full O(N) list traversal. Use pattern matching [_ | _] for non-empty lists or [] for empty lists instead.

Bad:

def process(items) when length(items) > 0 do
  ...
end

Good:

def process([_ | _] = items) do
  ...
end

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.