Skip to content

Latest commit

 

History

History
21 lines (13 loc) · 616 Bytes

BrokenOddnessCheck.md

File metadata and controls

21 lines (13 loc) · 616 Bytes

Pattern: Broken oddness check

Issue: -

Description

The code uses x % 2 == 1 to check to see if a value is odd, but this won't work for negative numbers (e.g., (-5) % 2 == -1). If this code is intending to check for oddness, consider using x & 1 == 1, or x % 2 != 0.

Examples:

if (x % 2 == 1) { }             // violation
if (method() % 2 == 1) { }      // violation

if (x & 1 == 1) { }             // OK
if (x % 2 != 0) { }             // OK

Further Reading