OptimumCredo.Check.Warning.RepoCallInEnumeration (OptimumCredo v0.5.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

Do not query the database once per element.

Bad

Enum.map(posts, fn post -> Repo.get(User, post.user_id) end)

Good

users = Repo.all(from u in User, where: u.id in ^Enum.map(posts, & &1.user_id))

A lexical heuristic: helper functions called from the loop are not followed.

Check-Specific Parameters

Use the following parameters to configure this check:

:repo_functions

Repo functions treated as queries.

This parameter defaults to [:get, :get!, :get_by, :get_by!, :one, :one!, :all, :exists?, :aggregate, :reload, :reload!, :preload].

:enumerator_functions

Enum/Stream functions whose function arguments run per element.

This parameter defaults to [:map, :flat_map, :each, :filter, :reject, :reduce, :reduce_while, :map_reduce, :find, :find_value, :any?, :all?, :count, :sort_by, :group_by, :min_by, :max_by, :map_every].

:ignore_paths

Directories (like test/) that are not checked.

This parameter defaults to ["test/", "priv/"].

General Parameters

Like with all checks, general params can be applied.

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