Credence.Pattern.NoMultipleEnumAt (credence v0.5.0)

Copy Markdown

Readability & performance rule: Detects multiple Enum.at/2 calls on the same variable with literal indices. Each Enum.at/2 traverses the list from the head, so N calls cost O(N × len). Pattern matching destructures the list in a single pass.

The rule fires when 3 or more Enum.at(var, literal) calls target the same variable, since that is a strong signal the code should use pattern matching instead.

Bad

sorted = Enum.sort(nums)
min1 = Enum.at(sorted, 0)
min2 = Enum.at(sorted, 1)
max1 = Enum.at(sorted, -1)
max2 = Enum.at(sorted, -2)

Good

sorted = Enum.sort(nums)
[min1, min2 | _] = sorted
[max1, max2 | _] = Enum.reverse(sorted)