
Inline adoc files when they are included exactly once. Also fix language tags because this inlining gives us better information on what language the code is written in.
66 lines
1.6 KiB
Plaintext
66 lines
1.6 KiB
Plaintext
== Why is this an issue?
|
|
|
|
Two "hash" classes, ``++Hashtable++``, and ``++ConcurrentHashMap++`` offer ``++contains++`` methods. One might naively assume that the ``++contains++`` method searches both keys and values for its argument. And one would be wrong. Because these legacy methods search only values, they are likely to mislead maintainers even if the original coder understood precisely what's going on.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,java]
|
|
----
|
|
Hashtable<String,Object> ht = new Hashtable<>();
|
|
// ...
|
|
|
|
if (ht.contains(foo)) { // Noncompliant
|
|
// ...
|
|
}
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,java]
|
|
----
|
|
Hashtable<String,Object> ht = new Hashtable<>();
|
|
// ...
|
|
|
|
if (ht.containsValue(foo)) {
|
|
// ...
|
|
}
|
|
----
|
|
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Use "containsValue" instead.
|
|
|
|
|
|
=== Highlighting
|
|
|
|
``++contains++``
|
|
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
=== on 14 Jul 2016, 20:39:16 Ann Campbell wrote:
|
|
https://github.com/google/error-prone/blob/master/docs/bugpattern/HashtableContains.md
|
|
|
|
=== on 15 Jul 2016, 09:48:30 Freddy Mallet wrote:
|
|
According to following statement [~ann.campbell.2] "they are likely to mislead maintainers even if the original coder understood precisely what's going on", this sounds to be more a Critical or Blocker Code Smells than a Bug.
|
|
|
|
=== on 15 Jul 2016, 14:21:25 Ann Campbell wrote:
|
|
I disagree [~freddy.mallet]. That statement gives the original coder the benefit of the doubt, but with a significant eye-roll. "_even if_ the original coder understood..."
|
|
|
|
|
|
I can reword if you like.
|
|
|
|
endif::env-github,rspecator-view[]
|