Skip to content

Add collection querying rule #962

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 19, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4690,6 +4690,54 @@ ary[..42]
ary[0..42]
----

=== Collection querying [[collection-querying]]

When possible, use https://docs.ruby-lang.org/en/master/Enumerable.html#module-Enumerable-label-Methods+for+Querying[predicate methods from `Enumerable`] rather than expressions with `#count`, `#length` or `#size`.

Querying methods express the intention more clearly and are more performant in some cases. For example, `articles.any?(&:published?)` is more readable than `articles.count(&:published?) > 0` and also more performant because `#any?` stops execution as soon as the first published article is found, while `#count` traverses the whole collection.

[source,ruby]
----
# bad
array.count > 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, there's also length to consider. ;-)

Copy link
Contributor Author

@lovro-bikic lovro-bikic Jun 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, updated. Note just that I wouldn't include length/size in the cop because it would yield too many false positives since Ruby has core classes which implement those methods, but aren't Enumerable (e.g. Integer, String, etc.).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I figured as much although probably this should be also mentioned in the cop's docs as well, and we can also provide "conservative/aggressive" modes there in case someone wants to do a broader search in their codebase (e.g. on a one off basis). Anyways, that's not relevant to the PR here.

array.length > 0
array.size > 0

array.count(&:something).positive?

array.count(&:something) == 0

array.count(&:something) == 1

# good
array.any?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we exclude this?
Would it be possible to write a separate guideline for this?
If we harvest real-world-ruby-apps repos, what usages of count will there be?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you think this should be separated?

For methods like count you should also keep in mind that it's a different method in Enumerable and ActiveRelation and it will probably be hard for us to tell them apart with a simple search.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, but if it’s not a list of repos of Rails apps like real-world-ruby-apps, chances are higher that it’s an Enumerable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using any? instead of count may work, but is a time bomb, which will blow up when falsey values start appearing in the data.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we just need a couple of examples illustrating the behavior with such values, perhaps in a sidebar/callout.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we harvest real-world-ruby-apps repos, what usages of count will there be?

@pirj I've attached a report for real-world-ruby-apps here: rubocop/rubocop#14288 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we just need a couple of examples illustrating the behavior with such values, perhaps in a sidebar/callout.

@bbatsov I've added this, let me know how it looks. I've also enabled Edits by maintainers in case you want to tweak something.


array.any?(&:something)

array.none?(&:something)

array.one?(&:something)
----

[NOTE]
--
Predicate methods without arguments can't replace `count` expressions when collection includes falsey values:
[source,ruby]
----
[nil, false].any?
# => false

[nil, false].none?
# => true

[nil].one?
# => false

[false].one?
# => false
----
--

== Numbers

=== Underscores in Numerics [[underscores-in-numerics]]
Expand Down