Credence.Pattern.PreferEnumSlice (credence v0.4.5)

Copy Markdown

Readability and Intent rule: Flags usage of Enum.drop/2 followed by Enum.take/2. Calling Enum.drop(list, start) piped into Enum.take(length) is a verbose way of slicing a collection. It can be confusing to read at a glance. Elixir provides Enum.slice/3, which explicitly communicates the intent of extracting a sublist and handles the operation cleanly.

Bad

graphemes
|> Enum.drop(best_window_start)
|> Enum.take(best_length)

Enum.take(Enum.drop(list, 5), 10)

Enum.drop(list, 5) |> Enum.take(10)

Good

graphemes
|> Enum.slice(best_window_start, best_length)

Enum.slice(list, 5, 10)