Instanceof Operator – Possible bad design

Was going through some forum posts, where i came across this interesting topic of Instanceof Operator, which made me think about it. The discussion point was about whether to use ‘instanceof’ operator or not in Java.

What is instanceof Operator?

instanceof opearator helps you to determine the type of the object.

InstanceOf Operator

Lets come to the point why its not advisable to use it and why your design may need a relook

From Effective C++, by Scott Meyers :

“Anytime you find yourself writing code of the form “if the object is of type T1, then do something, but if it’s of type T2, then do something else,” slap yourself.

Here is an example of the type of abuse Scott Meyers is speaking of :

/**
* Naive, incorrect use of instanceof.
*/
public final classBadInstanceOf {

  public static void doSomething(Animal aAnimal){
    if (aAnimal instanceof Fish){
      Fish fish = (Fish)aAnimal;
      fish.swim();
    }
    else if (aAnimal instanceof Spider){
      Spider spider = (Spider)aAnimal;
      spider.crawl();
    }
  }

  // PRIVATE //
  private static class Animal {}

  private static final class Fish extends Animal {
    void swim(){}
  }
  private static final class Spider extends Animal {
    void crawl(){}
  }
}

The mistake is corrected by using an overridable method :

/**
* Using polymorphism to call different methods.
* Does not use instanceof.
*/
public final classBadInstanceOfFixed {

  public static void main(String... aArgs){
    log("Starting...");
     Animal animal = new Animal();
     doSomething(animal);

     //repoint the same 'animal' reference to other objects :

     animal = new Fish();
     doSomething(animal);

     animal = new Spider();
     doSomething(animal);

     log("Done.");
  }

  /**
  * This method implementation doesn't care at all
  * about Fish/Spider. It just knows that it has
  * been passed an Animal. Different versions of
  * 'move' are called, specific to each Animal.
  */
  public static void doSomething(Animal aAnimal){
    aAnimal.move();
  }

 // PRIVATE //
  private static class Animal {
    void move(){
      log("Move like an animal...");
    }
  }

  private static final class Fish extends Animal {
    void move(){
      log("Move like a fish...");
    }
  }
  private static final class Spider extends Animal {
    void move(){
      log("Move like a spider...");
    }
  }

  private static void log(String aMessage){
    System.out.println(aMessage);
  }
}

An example run of this class :

Starting...
Move like an animal...
Move like a fish...
Move like a spider...
Done.

instanceof operator should only be used as a last resort where there is no other option available with you or only when the solution needs to be patched up and you don’t have time to redesign the system again. But always make sure that your next release removes it with a better design. Generally we tend to use it since its easy to use and easy to “visualize”. Another reason is that when a person thinks about the use case, he understands the conditions in terms of instanceof operator, like “if not this type then“, and tends to code in the same way.

Going through the example, its pretty easy to understand and visualize what exactly Scott Meyers is talking about and why he is asking to slap self 😉

So, wherever you have done the mistake, think again and see whether you can have the same problem solved using polymerphic behaviour.

Software Architect by profession, lazy programmer by choice.
Looking at the simplicity of problems and trying hard to find a complex solution for each of them.

“Opinions expressed are solely my own and do not express the views or opinions of my employer, family or friends”

Share if you liked this articleShare on Facebook0Share on Google+0Tweet about this on TwitterShare on LinkedIn0Email this to someone

So, What do you think?