-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4690,6 +4690,37 @@ 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 | ||
array.length > 0 | ||
array.size > 0 | ||
|
||
array.count(&:something).positive? | ||
|
||
array.count(&:something) == 0 | ||
|
||
array.count(&:something) == 1 | ||
|
||
# good | ||
array.any? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we exclude this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you think this should be separated? For methods like There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
||
array.any?(&:something) | ||
|
||
array.none?(&:something) | ||
|
||
array.one?(&:something) | ||
---- | ||
|
||
NOTE: Predicate methods without arguments aren't applicable when collection includes falsy values (e.g. `[nil, false].any?` evaluates to `false`). | ||
|
||
== Numbers | ||
|
||
=== Underscores in Numerics [[underscores-in-numerics]] | ||
|
There was a problem hiding this comment.
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. ;-)There was a problem hiding this comment.
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, File, etc.).There was a problem hiding this comment.
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.