SDA SE Wiki

Software Engineering for Smart Data Analytics & Smart Data Analytics for Software Engineering

User Tools

Site Tools


JT Bug number Jt-Cor-69

FindBugs does not detect this bug.

Inspired by

Original Description

“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).”

Detailed Description

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.

Sample Problem Scenario

<code Java>

public static void main(String []args) {

	    	int i=230;
	       System.out.println(i % 60 * 1000); 
	    }
	 

</Code>

Sample Counter Scenario

<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>

Sample Fix

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>

Evaluation Results

Benchmark project Time # Hits Precision Recall
FB JT FB JT Delta FB JT Delta
Apache Tomcat < 1s00 100% 100% 0 100% 100% 0
Argo UML < 1s00 100% 100% 0 100% 100% 0
AWT < 1s00 100% 100% 0 100% 100% 0
Jakarta < 1s00 100% 100% 0 100% 100% 0
Java IO < 1s00 100% 100% 0 100% 100% 0
JHotDraw < 1s00 100% 100% 0 100% 100% 0
jrefactory < 1s00 100% 100% 0 100% 100% 0
JServlet < 1s00 100% 100% 0 100% 100% 0
JUnit < 1s00 100% 100% 0 100% 100% 0
Lexi < 1s00 100% 100% 0 100% 100% 0
Mapper XML < 1s00 100% 100% 0 100% 100% 0
nutch < 1s00 100% 100% 0 100% 100% 0
PMD < 1s00 100% 100% 0 100% 100% 0
quickuml < 1s00 100% 100% 0 100% 100% 0

FB = FindBugs, JT = JTransformer, Delta = JTransformer - FindBug

All measurements were taken on a machine with the following properties:

  • JT Version: 4.0.0
  • Eclipse Kepler
  • Windows 8.1
  • i5 2,30Ghz
  • 8 GB Main Memory
  • SSD Hardrive
teaching/labs/mdse/2013/bug_descriptions/jt-bug-cor69.txt · Last modified: 2018/05/09 01:59 (external edit)

SEWiki, © 2023