[jdom-interest] instanceof

Elliotte Harold elharo at metalab.unc.edu
Tue Jan 23 08:42:28 PST 2007

Syloke Soong wrote:

> I am about to sit on a dead horse. Last month, we had a rather cute
> debate on the academics of the propriety of using instanceof and how
> anti-object-oriented it is. I have heard that some oo-purists feel
> instanceof should be banned or obliterated from the java language (or
> any language). I think use instanceof in java seems as anti-OO as the
> use of peace wreath for xmas is anti-American.

I think the arguments against instanceof are seriously misguided and 
based on tunnel vision. There's an implicit assumption in every argument 
against instanceof I have ever heard. This assumption is that the code 
is a monolithic code base in which all classes are controlled and 
modifiable. It does not consider the more realistic world in which many 
important classes are outside the developer's immediate control, and 
whose source code may not be modified. Thus polymorphism is not always 
an option.

In Java and other strongly statically typed languages instanceof or 
equivalent is a necessity and cannot be replaced by polymorphism. Using 
  instanceof in small closed systems consisting only of your own code 
may be a code smell.  Using instanceof in large systems that import 
third party libraries is not.

But then I prefer peace wreaths to Santa Clause too. :-)

Elliotte Rusty Harold  elharo at metalab.unc.edu
Java I/O 2nd Edition Just Published!

More information about the jdom-interest mailing list