Skip to content

Conversation

@jeffkreeftmeijer
Copy link
Member

@jeffkreeftmeijer jeffkreeftmeijer commented Dec 5, 2025

Replace expensive length/1 calls with more performant alternatives to fix Credo warnings flagged in CI1.

  • In scheduler.ex: Use pattern matching [_ | _] instead of length(events) > 0 in guard clause
  • In test files: Replace assert length(stack) > 0 with refute Enum.empty?(stack) for clearer intent and better performance

In Elixir, length/1 is O(n) because lists are implemented as linked lists, requiring traversal of the entire list to count elements. For emptiness checks, pattern matching against [] or [_ | _] is O(1) and Enum.empty?/1
stops at the first element, making them much more efficient than counting all elements just to compare with zero.

These changes maintain the same functionality while following Elixir best practices for checking list emptiness.

@jeffkreeftmeijer jeffkreeftmeijer self-assigned this Dec 5, 2025
@jeffkreeftmeijer jeffkreeftmeijer added the enhancement An improvement to an existing feature. label Dec 5, 2025
@backlog-helper
Copy link

backlog-helper bot commented Dec 5, 2025

Hi @jeffkreeftmeijer,

We've found some issues with your Pull Request.

  • This Pull Request does not include a changeset. Add a changeset if the change impacts users and should be included in the changelog upon release. Read more about changesets.
    Ignore this rule by adding [skip changeset] to your Pull Request body. - (More info)

New issue guide | Backlog management | Rules | Feedback

Replace expensive length/1 calls with more performant alternatives
to fix Credo warnings flagged in CI[1].

- In scheduler.ex: Use pattern matching [_ | _] instead of
  length(events) > 0 in guard clause
- In test files: Replace assert length(stack) > 0 with
  refute Enum.empty?(stack) for clearer intent and better performance

In Elixir, length/1 is O(n) because lists are implemented as linked
lists, requiring traversal of the entire list to count elements.
For emptiness checks, pattern matching against [] or [_ | _] is O(1)
and Enum.empty?/1 stops at the first element, making them much more
efficient than counting all elements just to compare with zero.

These changes maintain the same functionality while following Elixir
best practices for checking list emptiness.

[1]: https://github.com/appsignal/appsignal-elixir/actions/runs/19948488321/job/57203328408

[skip changeset]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement An improvement to an existing feature.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants