Credence.Pattern.NoSortThenReverse (credence v0.4.2)

Copy Markdown

Performance & readability rule: Detects the pattern of calling Enum.sort/1,2 followed by Enum.reverse/1 on the result.

Sorting ascending then reversing is equivalent to Enum.sort(list, :desc) but wastes a full O(n) pass for the reversal.

This rule can automatically fix pipeline and nested-call patterns where the sort uses a simple direction (:asc, :desc, or default ascending). Sorts with a custom comparator function are detected but left for manual fixing.

Bad

# In a pipeline
nums |> Enum.sort() |> Enum.reverse()

# As a nested call
Enum.reverse(Enum.sort(nums))

Good

nums |> Enum.sort(:desc)
Enum.sort(nums, :desc)