views:

298

answers:

3

Is type checking considered bad practice even if you are checking against an interface? I understand that you should always program to an interface and not an implementation - is this what it means?

For example, in PHP, is the following OK?

if($class instanceof AnInterface) {
   // Do some code
}

Or is there a better way of altering the behaviour of code based on a class type?

Edit: Just to be clear I am talking about checking whether a class implements an interface not just that it is an instance of a certain class.

A: 

If you can avoid type checking you should; however, one scenario where I found it handy, was we had a web service which took a message but the contents of the message could change. We had to persist the message back into a db, in order to get the right component to break the message down to its proper tables we used type checking in a sense.

What I find more common and flexible then if ($class instanceof SomeOtherType) is to define an IProcessing strategy for example and then using factory based on the type $class create the correct class.

So in c# roughly this:

void Process(Message msg)
{
    IProcessor processor=ProcessignFactory.GetProcessor(msg.GetType()); 
    processor.Process(msg); 
}

However sometimes doing this can be overkill if your only dealing with one variation that won't change implement it using a type check, and when / if you find you were wrong and it requires more checks then refactor it into a more robust solution.

JoshBerke
+2  A: 

As long as you follow the LSP, I don't see a problem. Your code must work with any implementation of the interface. It's not a problem that certain implementations cause you to follow different code paths, as long as you can correctly work with any implementation of the interface.

If your code doesn't work with all implementations of the interface, then you shouldn't use the interface in the first place.

jalf
That's a good point, but it is easy to put yourself in a situation where you have to update the method every time a new subclass is added if you do type checking. That should be avoided in favor of polymorphism.
Brian Rasmussen
Surely as long as the subclass implements the interface then no alterations should need to be made?
Tim Wardle
A: 

In my practice any checking for type (as well as type casting) has always indicated that something is wrong with the code or with the language.

So I try to avoid it whenever possible.

Rinat Abdullin