Skip to content

Disable the NumericPredicate cop#33

Open
ygnessin wants to merge 2 commits into
masterfrom
disable-numeric-predicate
Open

Disable the NumericPredicate cop#33
ygnessin wants to merge 2 commits into
masterfrom
disable-numeric-predicate

Conversation

@ygnessin

Copy link
Copy Markdown

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
Copy Markdown
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