Credence.Pattern.PreferLookupForDigitConversion (credence v0.8.0)

Copy Markdown

Detects the anti-pattern of mapping each hex digit (0–15) to its character representation via 16 separate function clauses, and rewrites it to a single clause using String.at/2 on the lookup string "0123456789ABCDEF".

Bad

defp hex_digit(0), do: "0"
defp hex_digit(1), do: "1"
defp hex_digit(2), do: "2"
defp hex_digit(3), do: "3"
defp hex_digit(4), do: "4"
defp hex_digit(5), do: "5"
defp hex_digit(6), do: "6"
defp hex_digit(7), do: "7"
defp hex_digit(8), do: "8"
defp hex_digit(9), do: "9"
defp hex_digit(10), do: "A"
defp hex_digit(11), do: "B"
defp hex_digit(12), do: "C"
defp hex_digit(13), do: "D"
defp hex_digit(14), do: "E"
defp hex_digit(15), do: "F"

Good

defp hex_digit(remainder) do
  "0123456789ABCDEF"
  |> String.at(remainder)
end

The 16-clause form is verbose and hard to maintain. A single string-lookup clause is more concise, idiomatic, and equally efficient.