-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathWhitespaceContradictsPrecedence.qhelp
More file actions
53 lines (42 loc) · 1.33 KB
/
WhitespaceContradictsPrecedence.qhelp
File metadata and controls
53 lines (42 loc) · 1.33 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>
Nested expressions where the spacing around operators suggests a different
grouping than that imposed by the Java operator precedence rules are problematic:
they could indicate a bug where the author of the code misunderstood the precedence
rules. Even if there is no a bug, the spacing could be confusing to people who
read the code.
</p>
</overview>
<recommendation>
<p>
Make sure that the spacing around operators reflects operator precedence, or use parentheses to
clarify grouping.
</p>
</recommendation>
<example>
<p>
Consider the following piece of code for allocating an array:
</p>
<pre>
int[] buf = new int[capacity + capacity>>1];
</pre>
<p>
Here, the spacing around <code>+</code> and <code>>></code> suggests the grouping
<code>capacity + (capacity>>1)</code>, that is, the allocated array should be 50% larger than
the given capacity.
</p>
<p>In fact, however, <code>+</code> has higher precedence than <code>>></code>, so
this code allocates an array of size <code>(capacity + capacity) >> 1</code>, which is
the same as <code>capacity</code>.
</p>
</example>
<references>
<li>
J. Bloch and N. Gafter, <em>Java Puzzlers: Traps, Pitfalls, and Corner Cases</em>, Puzzle 35. Addison-Wesley, 2005.
</li>
</references>
</qhelp>