https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112014-08-28T12:29:10ZRuby Issue Tracking SystemRuby master - Feature #10177: Hash#has_key? and Hash#has_value? should be deprecatedhttps://bugs.ruby-lang.org/issues/10177?journal_id=485292014-08-28T12:29:10ZEregon (Benoit Daloze)
<ul></ul><p>I guess your second sentence is meant the other way round:</p>
<p>"And I agree with him, I supposed Hash#key? should be preferred over Hash#has_key?, "</p> Ruby master - Feature #10177: Hash#has_key? and Hash#has_value? should be deprecatedhttps://bugs.ruby-lang.org/issues/10177?journal_id=485322014-08-28T20:14:28Zgogotanaka (Kazuki Tanaka)mail@tanakakazuki.com
<ul></ul><p>@ Mr. Benoit Daloze</p>
<p>oh.. Yes, that's what I meant!</p>
<p>Thank you.</p> Ruby master - Feature #10177: Hash#has_key? and Hash#has_value? should be deprecatedhttps://bugs.ruby-lang.org/issues/10177?journal_id=485602014-08-29T18:36:34Zshevegen (Robert A. Heiler)shevegen@gmail.com
<ul></ul><p>I liked has_key? but if matz prefers the other way I am fine,<br>
and I like that there is a consistent definition there.</p>
<p>Making stdlib / corelib more consistent would be great, and hopefully<br>
gem-installable in a modular way too, so</p>
<p>+1</p>
<p>for this.</p>
<p>I'll copy paste Matz's remark here again, just so that others don't<br>
have to click an external link:</p>
<p>"The basic naming for methods in standard class libraries are:</p>
<ul>
<li>use basic form (include not includes)</li>
<li>put question mark for predicates</li>
<li>put bang mark for "dangerous" version of methods</li>
</ul>
<p>"is_a" and "has_key" are exceptions. "is_a" (or "isa") used very often for inheritance in OO context.<br>
"has_key" has already been deprecated by "key?"</p>
<p>Besides that, backward incompatibility introduced by renaming them would be unbearable."</p>
<p>(Note: I think matz actually meant "has_key?" rather than "has_key" in that email back then)</p>
<p>If this is changed and a new release of ruby is done, please don't forget to comment<br>
on this in the changelog, so I can update my own ruby docu as well. Thanks!)</p> Ruby master - Feature #10177: Hash#has_key? and Hash#has_value? should be deprecatedhttps://bugs.ruby-lang.org/issues/10177?journal_id=487032014-09-07T20:33:38Zrits (First Last)
<ul></ul><p>in the interest of maximizing the English readability and sensibility (self documentability) of predicates, it is key? that should be deprecated.</p>
<p>when you have an instance method predicate in the form receiver.noun? this reads in English as "Receiver, are you a noun?", when in the form receiver.adjective?, "Receiver, are you adjective?" There is simply not enough information in a single noun or adjective to ask anything else.</p>
<p>So hash.key? reads, "hash, are you a key?"</p>
<p>If you want to ask a question of the receiver other than "Are you a noun?" or "Are you adjective?" the predicate has to be modified to reflect the question.</p>
<p>has_key? is perfect, it has enough information to be self documenting, it also implies that grammatical object of the question (the key) needs to be passed in.</p> Ruby master - Feature #10177: Hash#has_key? and Hash#has_value? should be deprecatedhttps://bugs.ruby-lang.org/issues/10177?journal_id=487042014-09-07T23:42:54Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul></ul><p>I agree with First Last that has_key? is better than key? from an English readability standpoint. Not knowing the history but being aware of both methods, I've always used has_key? instead of key? for that reason.</p>
<p>In my opinion, the backwards compatibility cost of deprecating then removing either has_key? or key? outweighs the benefit of removing one and standardizing on the other.</p> Ruby master - Feature #10177: Hash#has_key? and Hash#has_value? should be deprecatedhttps://bugs.ruby-lang.org/issues/10177?journal_id=488922014-09-13T23:28:39Zgogotanaka (Kazuki Tanaka)mail@tanakakazuki.com
<ul></ul><p><a class="user active user-mention" href="https://bugs.ruby-lang.org/users/796">@robert (Robert Gleeson)</a> A. Heiler<br>
@First Last<br>
@Jeremy Evans</p>
<p>Thank you so much for replying. I could acquire new point of view. I really appreciate it.</p>
<p>I got your point why has_key? is better than key?.</p>
<p>As First Last said,</p>
<blockquote>
<p>So hash.key? reads, "hash, are you a key?"</p>
</blockquote>
<p>does make sense for me.</p>
<p>Through my thinking for some time, I came to the conclusion that has_key? is better than key? too</p>
<p>But next issue is, as Jeremy Evans said</p>
<blockquote>
<p>the backwards compatibility cost of deprecating then removing either has_key? or key? outweighs the benefit of removing one and standardizing on the other.</p>
</blockquote>
<p>In my opinion, removing one and standardizing on the other at Ruby2.2.x is painful.</p>
<p>But it's better to do that in the future, so we start to deprecate either one.</p>
<p>Thank you. gogo.</p> Ruby master - Feature #10177: Hash#has_key? and Hash#has_value? should be deprecatedhttps://bugs.ruby-lang.org/issues/10177?journal_id=488952014-09-14T04:15:52Zsawa (Tsuyoshi Sawada)
<ul></ul><p><code>has_key?</code> is one of the few exceptions to the naming convention in that it uses the third person singular present form of a verb. To keep the method names as general as possible, it is better to use the most general form of a verb and avoid third person singular present (which is what Matz has done besides the exceptions). In that respect, it is much better if <code>has_key?</code> is deprecated than <code>key?</code> is deprecated.</p>
<p>First Last, given hashes like the following, I don't understand how <code>has_key?</code> is better "in the interest of maximizing the English readability and sensibility" than <code>key?</code>.</p>
<pre><code>I = {name: "Yamada", gender: "male"}
I.has_key?(:name)
you = {name: "Tanaka", gender: "female"}
you.has_key?(:gender)
we = {location: "Tokyo", company: "Foo corporation"}
we.has_key?(:location)
</code></pre> Ruby master - Feature #10177: Hash#has_key? and Hash#has_value? should be deprecatedhttps://bugs.ruby-lang.org/issues/10177?journal_id=488962014-09-14T04:43:11Zrits (First Last)
<ul></ul><p>Tsuyoshi Sawada wrote:</p>
<blockquote>
<p>I don't understand how <code>has_key?</code> is better</p>
</blockquote>
<p>I already explained it, all I can do is restate it.</p>
<p>When the predicate consists of a just single noun or adjective, there is only enough information in it to ask one type of question (Are you?).</p>
<p>Are you a {noun}? (e.g. proc.lambda? => "proc, are you a lambda?")<br>
Are you an {adjective}? (e.g. array.empty? => "array, are you empty?")</p>
<p>It can't mean anything whatsoever (have you?, should you?, countless other inquiries), because then the predicate name would be inscrutable, which is the opposite of the purpose of naming.</p>
<p>If you want to ask something other than "Are you?" there needs to be more information in the method name (e.g. has_key?)</p>
<p>That the conjugation can be off is nothing, next to not knowing what question is being asked.</p> Ruby master - Feature #10177: Hash#has_key? and Hash#has_value? should be deprecatedhttps://bugs.ruby-lang.org/issues/10177?journal_id=488972014-09-14T05:00:24Zsawa (Tsuyoshi Sawada)
<ul></ul><p>First Last wrote:</p>
<blockquote>
<p>That the conjugation can be off is nothing, next to not knowing what question is being asked.</p>
</blockquote>
<p>Then, it is not "in the interest of maximizing the English readability and sensibility." It is a matter of semantics.</p> Ruby master - Feature #10177: Hash#has_key? and Hash#has_value? should be deprecatedhttps://bugs.ruby-lang.org/issues/10177?journal_id=489092014-09-14T18:29:12Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul></ul><p>I guess the culture gap (not language gap) is leading to imbroglio in this case.</p>
<p><a href="http://en.wikipedia.org/wiki/High-_and_low-context_cultures" class="external">http://en.wikipedia.org/wiki/High-_and_low-context_cultures</a></p>
<p>Many Japanese people feel comfortable with "hash.key?(:foo)", because they are in a higher-context culture.</p>
<p>--<br>
Yusuke Endoh <a href="mailto:mame@ruby-lang.org" class="email">mame@ruby-lang.org</a></p> Ruby master - Feature #10177: Hash#has_key? and Hash#has_value? should be deprecatedhttps://bugs.ruby-lang.org/issues/10177?journal_id=489162014-09-15T07:38:19Zspatulasnout (B Kelly)billk@cts.com
<ul></ul><p>Salutations,</p>
<p>First Last wrote:</p>
<blockquote>
<p>Tsuyoshi Sawada wrote:</p>
<blockquote>
<p>I don't understand how <code>has_key?</code> is better</p>
</blockquote>
<p>I already explained it, all I can do is restate it.</p>
<p>When the predicate consists of a just single noun or adjective, there is only enough information in it to ask one type of question (Are you?).</p>
</blockquote>
<p>I wonder if this might be a circumstance where the Unix<br>
philosophy of preferring shorter names for frequently used<br>
operations might apply.</p>
<p>#key? must be presumably one of the most common queries to<br>
Hash.</p>
<p>Regards,</p>
<p>Bill</p> Ruby master - Feature #10177: Hash#has_key? and Hash#has_value? should be deprecatedhttps://bugs.ruby-lang.org/issues/10177?journal_id=489792014-09-19T18:01:16Zavit (Andrew Vit)andrew@avit.ca
<ul></ul><p>+1 for <code>has_key?</code> from me.</p>
<p>It's more readable as natural English as pointed out earlier.</p>
<p><code>has_key?</code> is more clear because <code>key(value)</code> and <code>key?(key)</code> expect different arguments, so the names should be more clearly different. There is a common ruby idiom for <code>x? == !!x</code> as paired method names. In this case these are not at all similar.</p>
<p><code>has_key?</code> is less likely to conflict with other objects that define dynamic predicate methods, which is better for duck-typing dynamic <code>model.x?</code> objects when wrapping it with method_missing for example. (OpenStruct, ActiveModel, ActiveSupport::StringInquirer are just a few examples.) This common use of predicate methods also has zero arity which is different from <code>key?</code> / <code>has_key?</code>.</p>