-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathWriteOnlyContainer.qhelp
More file actions
53 lines (40 loc) · 1.47 KB
/
WriteOnlyContainer.qhelp
File metadata and controls
53 lines (40 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>
If the contents of a collection or map are never accessed in any way, then it is useless and
the code that updates it is effectively dead code. Often, such objects are left over from
an incomplete refactoring, or they indicate an underlying logic error.
</p>
</overview>
<recommendation>
<p>
Either remove the collection/map if it is genuinely unnecessary, or ensure that its
elements are accessed.
</p>
</recommendation>
<example>
<p>
In the following example code, the <code>reachable</code> method determines whether a node in
a tree is reachable from <code>ROOT</code>. It maintains a set <code>reachableNodes</code>,
which contains all nodes that have previously been found to be reachable. Most likely, this set
is meant to act as a cache to avoid spurious recomputation, but as it stands the code never checks
whether any node is contained in the set.
</p>
<sample src="WriteOnlyContainer.java" />
<p>
In the following modification of the above example, <code>reachable</code> checks the cache
to see whether the node has already been considered.
</p>
<sample src="WriteOnlyContainerGood.java" />
</example>
<references>
<li>
Java API Specification:
<a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Collection.html">Collection</a>,
<a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Map.html">Map</a>.
</li>
</references>
</qhelp>