OPT.INSOF
Test "instanceof" to an interface
Description
This rule flags code that performs "instanceof" tests to a class rather than to an interface.
Because interface-based design allows for the flexible inclusion of different implementations, it is generally beneficial. Whenever possible, perform "instanceof" tests to an interface rather than a class.
Example
package OPT;
public class INSOF {
private void method (Object o) {
if (o instanceof InterfaceBase) { } // better
if (o instanceof ClassBase) { } // worse.
}
}
class ClassBase {}
interface InterfaceBase {}
Reference
Graig Larman, Rhett Guthrie. Java 2 Performance and Idiom Guide. Prentice Hall, 2000, p.207.
|