Credence.Pattern.PreferCondForNestedIf (credence v0.8.0)

Copy Markdown

Detects nested if/else blocks where the else branch contains another if (with its own else), and flattens them into a cond for readability.

Bad

if x > 0 do
  "positive"
else
  if x < 0 do
    "negative"
  else
    "zero"
  end
end

Good

cond do
  x > 0 -> "positive"
  x < 0 -> "negative"
  true -> "zero"
end

Auto-fix

Flattens the nested if/else into a single cond expression. The outer condition becomes the first clause, the inner condition becomes the second clause, and the inner else body becomes the catch-all true clause.

Safety

The transformation is behaviour-preserving: both forms evaluate conditions in the same top-to-bottom order, and both only evaluate the branch body for the first truthy condition. The true catch-all mirrors the inner else, which handles all remaining cases.