Monday, January 09, 2006

Sean Corfield Spawns More Interface Debate…

The debate on whether or not ColdFusion needs interfaces returns, headed up by a really good post by Sean Corfield. There’s some excellent discussion going on in the replies, I’d say this is a definite must read thread. As good as many of the points are, however, I’m not swaying, I want interfaces, and as soon as I get my thoughts together, I’ll show some examples of how they can be used very effectively. One thing I would like to throw in is that interfaces aren’t only about typing and providing a contract for your objects. Interfaces can give your objects sudo-multiple inheritance. Lets say your object extends one base class, but also satisfies the methods for two interfaces, if you have methods in some objects that take arguments of any of those three types, four including the type of the composite object, that object can satisfy any of those type constraints, thus acting as 4 different objects from the point of view of the various methods. If we set the argument types to ‘any’ or ‘WEB-INF.cftags.component’, that method will accept any object, and we void all contractual obligations of our objects, no matter how conceptual they really are in ColdFusion.


Anonymous Anonymous said...

But that's just it, even if they ADDED interfaces ALL they would be able to provide is runtime type checking. So in your example, an object may or may not satisfy the requirements of 4 interfaces, but that is irrelevant because the type checking doesn't happen until runtime. So if you pass a CFC that doesn't implement the correct interface, at runtime you'd get an error message saying that. But if you don't have interfaces at all and you pass a CFC and try to call a method on it that doesn't exist, you'll also get an error. All interfaces would give us is a slightly more descriptive error.

Brian Kotek

11:55 AM  
Blogger Chris Scott said...

You're absolutely right that interfaces in CF don't buy you much as far as compile time checking. However that slightly more descriptive error, which points you to the fact that your class is not implementing some method, is pretty clearly telling you that your cfcs are not programmed in accordance to your design goals. I use interfaces and and abstract classes as part of my application design, it is crucial for me to know if cfcs fail to implement methods that are part of that design, regardless if it is done at compile time or runtime. I guess it really comes down to architectural decisions, and again, this is personal. Interfaces are a seriously hard sell for most people, I understand that completely!

1:33 PM  
Anonymous Anonymous said...

I don't think anyone would argue that interfaces and abstract classes, the very basis of polymorphism, are absolutely critical when designing an OO model. No one is saying "give up on polymorphism". The difference is in how your design is enforced. Since all CF type checking MUST be done at runtime, there is very little difference between an error telling you "CFC X does not implement Interface Y" and an error telling you "You tried to call method foo() that does not exist in CFC X". If you swtich to using type="any" or type="WEB-INF.cftags.component" you can still exploit polymorphism to your hearts content...more in fact, because you get around the single-inheritance limitation.

3:08 PM  
Anonymous Anonymous said...

Sorry that was from me again, Brian Kotek.

3:08 PM  
Blogger Chris Scott said...

Brian, I'm not trying to be the poster boy for interfaces or anything. But I do think at one point you agreed with me, no? Here's a comment you put on Joe Rinehart's blog in january about interfaces:

The only one that I feel is absolutely necessary is interfaces, and that is just to allow for proper and effective polymorphism that doesn't rely on inheritance. Since it would be simple to add and would only matter to the people who use it, I can't see why they would possibly choose to omit it again.
Posted By Brian Kotek / Posted At 5/16/05 1:15 PM

6:48 AM  

Post a Comment

<< Home