Credo.Check.Refactor.CondInsteadOfIfElse (Credo v1.7.17)

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

Prefer cond over if/else blocks.

So while this is fine:

if allowed? do
  :ok
end

The use of else could impact readability:

if allowed? do
  :ok
else
  :error
end

and could be improved to:

cond do
  allowed? -> :ok
  true -> :error
end

There's no technical reason for this; it's a matter of preferred code style.

NOTE: This check is mutually exclusive with Credo.Check.Refactor.CondStatements, which recommends the opposite. Enable only one of these checks.

Check-Specific Parameters

Use the following parameters to configure this check:

:allow_one_liners

Allow one-liner if/else expressions (e.g., if x, do: y, else: z).

This parameter defaults to false.

General Parameters

Like with all checks, general params can be applied.

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