Software Engineering for Smart Data Analytics & Smart Data Analytics for Software Engineering
“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
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.
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>
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>
Just removing the whole if-block fixes the problem.
Benchmark project | Precision | Recall | ||||
---|---|---|---|---|---|---|
FB | JT | Delta | FB | JT | Delta | |
Project … | …% | …% | …% | …% | …% | …% |
Project … | …% | …% | …% | …% | …% | …% |
FB = FindBugs, JT = JTransformer, Delta = JTransformer - FindBugs