Whitespace contradicts operator precedence¶
Make sure that the spacing around operators reflects operator precedence, or use parentheses to clarify grouping.
Consider the following piece of code for allocating an array:
var a = new Array(capacity + capacity>>1);
Here, the spacing around
>> suggests the grouping
capacity + (capacity>>1), that is, the allocated array should be 50% larger than the given capacity.
+ has higher precedence than
>>, so this code allocates an array of size
(capacity + capacity) >> 1, which is the same as
To fix this issue, parentheses should be used like this:
var a = new Array(capacity + (capacity>>1));
J. Bloch and N. Gafter, Java Puzzlers: Traps, Pitfalls, and Corner Cases, Puzzle 35. Addison-Wesley, 2005.
Common Weakness Enumeration: CWE-783.