Soundcheck.Check.Refactor.ExpensiveCallInTemplate (soundcheck v0.1.0)

Copy Markdown View Source

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

Flags database, HTTP, and sorting calls inside template expressions. LiveView re-evaluates an expression whenever an assign it reads changes, so the query or request runs again on every such change, and on every render when the expression reads no tracked assign.

Before:

<p>{Repo.aggregate(Post, :count)} posts</p>

After:

# in mount/3 or the event that changes the count
assign(socket, :post_count, Repo.aggregate(Post, :count))

<p>{@post_count} posts</p>

Modules match expensive_modules (atoms or regexes on the alias-expanded name); {Module, :function} pairs in expensive_functions add individual functions.

Confidence: medium. Tier: syntactic (works without compiling).

Check-Specific Parameters

Use the following parameters to configure this check:

:excluded_paths

Paths to skip (atoms, binaries as path segments, or regexes).

This parameter defaults to [~r/\/test\//, "test"].

:strict

Unused.

This parameter defaults to false.

:expensive_modules

Modules whose calls count as expensive: atoms or regexes.

This parameter defaults to [~r/\.Repo$/, ~r/^Ecto\.Query$/, ~r/^(Req|Finch|HTTPoison|Tesla)$/].

:expensive_functions

{Module, :function} pairs that count as expensive.

This parameter defaults to [{Enum, :sort}, {Enum, :sort_by}, {Enum, :group_by}, {Enum, :frequencies}].

General Parameters

Like with all checks, general params can be applied.

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