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

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

Use the following parameters to configure this check:

:exclude_test_files

Set to true to skip test files (default: false)

This parameter defaults to nil.

General Parameters

Like with all checks, general params can be applied.

Parameters can be configured via the .credo.exs config file.