Credence.Pattern.NoDoubleSortSameList (credence v0.4.3)

Copy Markdown

Performance rule: Detects sorting the same list twice — once ascending and once descending — when a single sort plus Enum.reverse/1 would suffice.

Enum.sort(list, :desc) is typically implemented as sort-then-reverse internally, so calling both Enum.sort(list) and Enum.sort(list, :desc) on the same variable performs two full O(n log n) sorts. Sorting once and reversing the result is O(n log n) + O(n).

Bad

asc = Enum.sort(arr)
desc = Enum.sort(arr, :desc)

Good

asc = Enum.sort(arr)
desc = Enum.reverse(asc)