SDA SE Wiki

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

User Tools

Site Tools


Bug Cor 4

Inspired by

Original Description

“This instanceof test will always return false. Although this is safe, make sure it isn't an indication of some misunderstanding or some other logic error. ” – 2013.10.28

Detailed Description

Often times instanceof is used to ensure a variable is NOT of a certain type. However, sometimes it is impossible that the variable can ever be of this type, therefore the check is redundant. It is not critical to do the check, but there might be a logical misunderstanding of the control flow.

Sample Problem Scenario

It is certain that the variable myList will be of type ArrayList. <code Java>

	List<Integer> myList = new ArrayList<Integer>();
	if(myList instanceof LinkedList){
		System.out.println("This will never happen");
	}

</Code>

Sample Counter Scenario

It is uncertain which implementation the variable myList will be of. Therefore, the check is useful. <code Java>

	List<Integer> myList = getObjectFromExternalAPI();
	if(myList instanceof ArrayList){
		System.out.println("This might happen");
	}

</Code>

Sample Fix

Just removing the whole if-block fixes the problem.

Evaluation Results

Benchmark project Precision Recall
FB JT Delta FB JT Delta
Project … …% …% …% …% …% …%
Project … …% …% …% …% …% …%

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

teaching/labs/mdse/2013/bug_descriptions/jt-bug-cor4.txt · Last modified: 2018/05/09 01:59 (external edit)

SEWiki, © 2023