rspec/rules/S3675/java/rule.adoc

28 lines
669 B
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
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.
2021-04-28 16:49:39 +02:00
== Noncompliant Code Example
----
Hashtable<String,Object> ht = new Hashtable<>();
// ...
if (ht.contains(foo)) { // Noncompliant
// ...
}
----
2021-04-28 16:49:39 +02:00
== Compliant Solution
----
Hashtable<String,Object> ht = new Hashtable<>();
// ...
if (ht.containsValue(foo)) {
// ...
}
----