-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathBadCheckOdd.qhelp
More file actions
45 lines (34 loc) · 1.14 KB
/
BadCheckOdd.qhelp
File metadata and controls
45 lines (34 loc) · 1.14 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
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>Avoid using <code>x % 2 == 1</code> or <code>x % 2 > 0</code>
to check whether a number <code>x</code> is odd, or
<code>x % 2 != 1</code> to check whether it is even.
Such code does not work for negative numbers.
For example, <code>-5 % 2</code> equals <code>-1</code>, not <code>1</code>.
</p>
</overview>
<recommendation>
<p>
Consider using <code>x % 2 != 0</code> to check for odd and <code>x % 2 == 0</code> to check for even.
</p>
</recommendation>
<example>
<p>-9 is an odd number but this example does not detect it as one. This is because <code>-9 % 2
</code> is -1, not 1.</p>
<sample src="BadCheckOdd.java" />
<p>It would be better to check if the number is even and then invert that check.</p>
<sample src="BadCheckOddGood.java" />
</example>
<references>
<li>
J. Bloch and N. Gafter, <em>Java Puzzlers: Traps, Pitfalls, and Corner Cases</em>, Puzzle 1. Addison-Wesley, 2005.
</li>
<li>
Java Language Specification:
<a href="https://docs.oracle.com/javase/specs/jls/se11/html/jls-15.html#jls-15.17.3">Remainder Operator %</a>.
</li>
</references>
</qhelp>