Skip to content
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

Disable the NumericPredicate cop #33

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ygnessin
Copy link

https://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Style/NumericPredicate

This cop does not support auto-correct, is generally noisy in our codebases,
and not that helpful. Reasoning for the default is dubious:

"The default is the predicate. The benefit of that is you'll avoid silent
errors when accidentally comparing to nil, and will instead get a runtime
error. The downside is people might not immediately know whether 0 is
considered positive or not."

I'm not sure about that reasoning because nil > 0 is not a silent error.
Also, the conditional style is more performance (on a micro scale)

https://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Style/NumericPredicate

This cop does not support auto-correct, is generally noisy in our codebases,
and not that helpful. Reasoning for the default is dubious:

"The default is the predicate. The benefit of that is you'll avoid silent
errors when accidentally comparing to nil, and will instead get a runtime
error. The downside is people might not immediately know whether 0 is
considered positive or not."
- rubocop/rubocop#5564 (comment)

I'm not sure about that reasoning because `nil > 0` is not a silent error.
Also, the conditional style is more performance (on a micro scale)
@ygnessin ygnessin requested a review from mtchavez April 11, 2019 02:28
@ygnessin
Copy link
Author

Ah, after posting this I figured out what it's talking about:

2.6.1 :009 > nil == 0
 => false 
2.6.1 :010 > nil.zero?
Traceback (most recent call last):
        4: from /Users/yuval/.rvm/rubies/ruby-2.6.1/bin/irb:23:in `<main>'
        3: from /Users/yuval/.rvm/rubies/ruby-2.6.1/bin/irb:23:in `load'
        2: from /Users/yuval/.rvm/rubies/ruby-2.6.1/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb:11:in `<top (required)>'
        1: from (irb):10
NoMethodError (undefined method `zero?' for nil:NilClass)

If the rest of the team thinks this difference is meaningful then I could understand wanting to keep this cop. However, it is scary to think that converting x == 0 to x.zero? is a functional change and may introduce bugs if we "just do it"; that's probably why the cop doesn't support autocorrect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant