ForgeCredoChecks.NoCaseTrueFalse (forge_credo_checks v0.4.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 high and works with any version of Elixir.

Explanation

case on a boolean expression with true/false clauses should be if/else.

Why

LLMs frequently transliterate Python's if/else through a case on a boolean expression with explicit true/false clauses. Idiomatic Elixir uses if/else when the condition is already a boolean — the reader doesn't have to scan to determine which clause is the truthy branch.

Only case whose subject is a boolean expression (comparison, operator, function call) is flagged. A case on a plain variable is assumed to be a legitimate pattern match on a tristate value (e.g. nil plus the booleans) and is not flagged.

Bad

case rem(n, 2) == 0 do
  true -> :even
  false -> :odd
end

Good

if rem(n, 2) == 0 do
  :even
else
  :odd
end

Also flagged

case expr do true -> a; _ -> b end
case expr do false -> b; _ -> a end

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.