# `Soundcheck.Check.Refactor.ExpensiveCallInTemplate`
[🔗](https://github.com/leonqadirie/soundcheck/blob/v0.1.0/lib/soundcheck/check/refactor/expensive_call_in_template.ex#L1)

## Basics

> #### This check is disabled by default. {: .neutral}
>
> [Learn how to enable it](`e:credo:config_file.html#checks`) 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](`e:credo:check_params.html`) can be applied.

Parameters can be configured via the [`.credo.exs` config file](`e:credo:config_file.html`).

---

*Consult [api-reference.md](api-reference.md) for complete listing*
