Amount of type checks in Java

Martin Fowler writes about DynamicTypeCheck:

Recently some of our developers ran into the accusation that with a dynamic language like ruby you use so many dynamic type checks that you end up effectively writing your own type system. So they thought, since we’ve written a lot of real ruby code – how often do we make dynamic type checks?

We define a dynamic type check as the use of the methods is_a?, kind_of?, and instance_of?.

This made me think about the use of instanceof in Java. A pretty non-scientific investigation of an Open Source Java application (Vuze) showed:

Lines of code: 740257 (find . -name '*.java' | xargs cat |wc -l)

Number of instanceof: 1926 (find . -name '*.java' | xargs cat| grep -c ' instanceof ')

LOC/Number of instanceof: 384

So, there is actually more instanceof in this Java project than there are dynamic type checks in the anonymous Ruby projects used for the statistics presented by Martin Fowler. I certainly hope it wasn’t a Java developer that accused dynamic languages to use lots of dynamic type checks!

Leave a Reply

Your email address will not be published. Required fields are marked *