Software Engineering for Smart Data Analytics & Smart Data Analytics for Software Engineering
“The code multiplies the result of an integer remaining by an integer constant. Be sure you don't have your operator precedence confused. For example i % 60 * 1000 is (i % 60) * 1000, not i % (60 * 1000).”
For Java remainders and multiplications are on the same level when it comes to operator precedence (i.e. whatever term stands further left is computed first). That might be not very intuitive or widely known and can therefore lead to errors. Programmers should always indicate the order they want to have using parentheses.
<code Java>
public static void main(String []args) {
int i=230; System.out.println(i % 60 * 1000); }
</Code>
<code Java>
public static void main(String []args) {
int i=230; System.out.println("i % 60 * 1000"); System.out.println(60 % 1000); System.out.println(i * 60); System.out.println(i * 60 + 1000); System.out.println(i * (60 % 1000)); }
</Code>
It's hard to provide a fix since it is not sure what the programmer wants. In any case she should specify the operations using Parentheses. One of the following “fixes” can be applied: <code Java>
public static void main(String []args) { int i=230; System.out.println((i % 60) * 1000); }
</Code> <code Java>
public static void main(String []args) { int i=230; System.out.println(i % (60 * 1000)); }
</Code>
Benchmark project | Time | # Hits | Precision | Recall | |||||
---|---|---|---|---|---|---|---|---|---|
FB | JT | FB | JT | Delta | FB | JT | Delta | ||
Apache Tomcat | < 1s | 0 | 0 | 100% | 100% | 0 | 100% | 100% | 0 |
Argo UML | < 1s | 0 | 0 | 100% | 100% | 0 | 100% | 100% | 0 |
AWT | < 1s | 0 | 0 | 100% | 100% | 0 | 100% | 100% | 0 |
Jakarta | < 1s | 0 | 0 | 100% | 100% | 0 | 100% | 100% | 0 |
Java IO | < 1s | 0 | 0 | 100% | 100% | 0 | 100% | 100% | 0 |
JHotDraw | < 1s | 0 | 0 | 100% | 100% | 0 | 100% | 100% | 0 |
jrefactory | < 1s | 0 | 0 | 100% | 100% | 0 | 100% | 100% | 0 |
JServlet | < 1s | 0 | 0 | 100% | 100% | 0 | 100% | 100% | 0 |
JUnit | < 1s | 0 | 0 | 100% | 100% | 0 | 100% | 100% | 0 |
Lexi | < 1s | 0 | 0 | 100% | 100% | 0 | 100% | 100% | 0 |
Mapper XML | < 1s | 0 | 0 | 100% | 100% | 0 | 100% | 100% | 0 |
nutch | < 1s | 0 | 0 | 100% | 100% | 0 | 100% | 100% | 0 |
PMD | < 1s | 0 | 0 | 100% | 100% | 0 | 100% | 100% | 0 |
quickuml | < 1s | 0 | 0 | 100% | 100% | 0 | 100% | 100% | 0 |
FB = FindBugs, JT = JTransformer, Delta = JTransformer - FindBug
All measurements were taken on a machine with the following properties: