Rubocop: Customizing the preference of has_key? vs key?

Created on 19 Aug 2016  路  8Comments  路  Source: rubocop-hq/rubocop

Matz and rubocop prefer key? and value? to has_key? and has_value?. This is a step backwards in readability. People rarely walk up to another person and just say key?, but when they do, they also give some other cue like holding out a hand or inflecting their voice. Simply saying key? is ambiguous as it could mean "do you have the key?", "did you ever have the key?", "can I have the key?", etc...

IMHO, adding a question mark to the end of a method is not a substitute for dropping a meaningful prefix, but rubyists have a convention of dropping "is" and "has" from the start of method names and many people like it.

I'm trying to configure this cop to prefer has_key? and has_value?, but I'm not sure if this is possible. (I don't want to disable it outright because there may be other method suggestions that I would like to know about in the future.) I didn't see an example of how to customize this in the config/default.yml, so I tried customizing this with :

Style/PreferredHashMethods:
  PreferredMethods:
    key?: 'has_key?'
    value?: 'has_value?'

I got the following error output

Warning: unrecognized parameter Style/PreferredHashMethods:PreferredMethods found in /home/me/Documents/dev/foo/config/rubocop.yml

Is customizing this cop supported?

RuboCop version

0.42.0 (using Parser 2.3.1.2, running on ruby 2.3.1 x86_64-linux)

enhancement hacktoberfest

Most helpful comment

In this thread, Matz mentions that it won't be removed (even in Rails 3). At this point, it's not really deprecated. The Ruby team prefers key? to has_key? which is probably why key? has ended up as the rubocop recommendation. As someone pointed out in this thread key? doesn't really make sense. hash.key? reads to native English speakers as "hash, are you a key?", not "hash, do you have a key?". has_key? is preferred for clarity and readability.

All 8 comments

This cop is not configurable, but I agree that it should be.

I thought that has_key? was deprecated? It seems like there is a bit of churn back and forth on if it should be deprecated or not. Either way, it has been several years and there doesn't seem to be any progress in someone actually removing the methods if they in fact deprecated.

In this thread, Matz mentions that it won't be removed (even in Rails 3). At this point, it's not really deprecated. The Ruby team prefers key? to has_key? which is probably why key? has ended up as the rubocop recommendation. As someone pointed out in this thread key? doesn't really make sense. hash.key? reads to native English speakers as "hash, are you a key?", not "hash, do you have a key?". has_key? is preferred for clarity and readability.

I would like to tackle this issue for hacktoberfest.

Great!

Sorry. I feel like I overcommitted in the moment. I haven鈥檛 had time to work on this issue. I wanted to let you all know. Thanks.

@johndavidmartinez: Don't worry about it. You're welcome back when you think you have the time. 馃榾

Hope you don't mind @johndavidmartinez, just came across this earlier and was keen to see it included. #3579 includes options for short (default) and verbose hash function name styles.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lepieru picture lepieru  路  3Comments

deivid-rodriguez picture deivid-rodriguez  路  3Comments

NobodysNightmare picture NobodysNightmare  路  3Comments

bquorning picture bquorning  路  3Comments

david942j picture david942j  路  3Comments