KogenCredo.Check.Warning.RepoAllThenTrim (KogenCredo v0.1.0)

View Source

Basics

This check is disabled by default.

Learn how to enable it via .credo.exs.

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

Explanation

Repo.all/1 followed by Enum.take/uniq_by/slice/filter/at loads every matching row, then discards most of them in memory. Every call pays for the whole table slice.

Bad:

Query |> where([q], q.user_id == ^id) |> Repo.all() |> Enum.take(10)

Good:

Query |> where([q], q.user_id == ^id) |> limit(10) |> Repo.all()

Use group_by/distinct plus limit instead of Enum.uniq_by/2 plus Enum.take/2. Heuristic: the check cannot tell that a result set is small.

Check-Specific Parameters

Use the following parameters to configure this check:

:trimmer_functions

Enum functions that count as in-memory trimming (atoms).

This parameter defaults to [:take, :uniq_by, :slice, :filter, :at].

:repo_names

Last module segments accepted as a repo (default ["Repo"]).

This parameter defaults to ["Repo"].

:repo_modules

Full repo module names accepted as a repo (strings).

This parameter defaults to [].

:included_paths

Globs of files to check; empty means every file.

This parameter defaults to [].

:excluded_paths

Globs of files to skip (default ["test/**"]).

This parameter defaults to ["test/**"].

General Parameters

Like with all checks, general params can be applied.

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