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

Allow methods prefixed with has_ #6

Closed
codener opened this issue Mar 4, 2019 · 2 comments
Closed

Allow methods prefixed with has_ #6

codener opened this issue Mar 4, 2019 · 2 comments

Comments

@codener
Copy link
Member

codener commented Mar 4, 2019

Rubocop won't allow a method named has_replies?:

class Message
  def reply?
    # return whether message IS a reply
  end

  def has_replies?
    # return whether message HAS any replies
  end
  ...
end

# => app/models/message.rb:28:7: C: Naming/PredicateName: Rename has_replies? to replies?

However, changing this would make the model quite hard to understand. #reply? and #replies? sound so similar, yet have quite a different meaning. Furthermore, replies could also be meant as 3rd person ("it replies"), not plural.

Verdict

I believe we should not force both property ("is") and possession ("has") into unprefixed query methods. While an "is_" prefix is not needed in Ruby, the "has_" prefix definitely has its place.

@foobear
Copy link
Member

foobear commented Mar 15, 2019

FYI: The cop steps from the idea of avoiding redundant and inconsistent method name prefixes which don't match core Ruby naming (e.g. has_key? became key? a while ago).

I personally don't consider similarity issues a valid point here and would be perfectly fine with replies? as a means of expressing "this message has replies", or even prefer any_replies? because it more clearly expresses what you actually mean (IMO).

However, I agree that there are are reasons for has_-prefixed method names and suggest we adjust the method's NamePrefixBlacklist accordingly. An is_ prefix should still be a violation.

@foobear
Copy link
Member

foobear commented Apr 3, 2019

Cop has been disabled in 3.1.0

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

No branches or pull requests

2 participants