Bylaw.Credo.Check.Elixir.NoParamExtractionInFunctionHead (bylaw_credo v0.1.0-alpha.1)

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

Prevents destructuring map/struct parameters in function heads just to extract values.

Examples

Avoid:

  def perform(%Oban.Job{args: args, attempt: attempt, max_attempts: max_attempts}) do
    ...
  end

  def create_user(%{email: email, role: role} = attrs) do
    ...
  end

  def perform(%Oban.Job{args: %{"invoice_id" => invoice_id, "tenant_id" => tenant_id}}) do
    ...
  end

  Enum.map(jobs, fn %Oban.Job{args: args} -> process(args) end)

Prefer:

  def perform(%Oban.Job{} = job) do
    with {:ok, invoice_id} <- fetch_required_arg(job.args, "invoice_id", :missing_invoice_id),
         {:ok, tenant_id} <- fetch_required_arg(job.args, "tenant_id", :missing_tenant_id) do
      ...
    end
  end

  def create_user(attrs) do
    with {:ok, email} <- Map.fetch(attrs, :email) do
      role = Map.get(attrs, :role, :member)
      ...
    end
  end

  Enum.map(jobs, fn %Oban.Job{} = job -> process(job.args) end)

Notes

Function heads have two jobs: naming parameters and selecting which clause runs. When they also pull values out of maps and structs, clause selection and data extraction become harder to distinguish.

Moving extraction into the function body keeps the head focused on what the clause accepts and makes missing-data handling more explicit.

Pattern matching in function heads is fine when it does real dispatch work - deciding which clause runs, not pulling data out for later use.

Examples of dispatch-oriented pattern matching:

  def fetch_user(nil), do: {:error, :missing_user_id}
  def fetch_user(user_id), do: Accounts.get_user(user_id)

  def process(%{type: :email} = notification), do: send_email(notification)
  def process(%{type: :sms} = notification), do: send_sms(notification)

  def perform(%Oban.Job{args: %{"invoice_id" => _}} = job) do
    invoice_id = Map.fetch!(job.args, "invoice_id")
    ...
  end

  def perform(%Oban.Job{}), do: {:discard, :missing_invoice_id}

  def create(conn, %{"_json" => list}) when is_list(list) do
    ...
  end

  def handle_result({:ok, value}), do: {:ok, value}
  def handle_result({:error, reason}), do: {:error, reason}

This check uses static AST analysis, so it favors clear source-level patterns over runtime behavior.

Options

This check has no check-specific options. Configure it with an empty option list.

Usage

Add this check to Credo's checks: list in .credo.exs:

%{
  configs: [
    %{
      name: "default",
      checks: [
        {Bylaw.Credo.Check.Elixir.NoParamExtractionInFunctionHead, []}
      ]
    }
  ]
}

Check-Specific Parameters

There are no specific parameters for this check.

General Parameters

Like with all checks, general params can be applied.

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