Credence.Rule.NoEnumTakeNegative
(credence v0.2.0)
Copy Markdown
Performance rule: Detects Enum.take(list, -n) where n is a positive
integer literal.
For linked lists, Enum.take(list, -n) must internally determine the list
length, then traverse again to the cut point — effectively two full
traversals. If the list was just sorted, sorting in the opposite direction
and taking a positive count is more efficient.
Bad
sorted = Enum.sort(nums)
top_three = Enum.take(sorted, -3)Good
top_three = Enum.sort(nums, :desc) |> Enum.take(3)