ColdFusion: Do we *need* interfaces?
Posted by Joe Rinehart at 9:03 AM
13 comments - Categories:
ColdFusion MX
One of the most requested features from the OO CF crowd has been the implementation of interfaces in ColdFusion. Sean Corfield has seems to be on the "don't need them" side of the fence nowadays and has sparked a lot of comments (a *lot*) with his entry on the subject. While I was for them for a while, I switched over last May, and thought it might be a good time to re-run my old blog entry on the subject:
Updated 11:05PM EST, Jan 11: I've shut off comments on this post, as it's becoming the same discussion as is on Corfield.org. Please visit Sean's Post to chime in!
Originally blogged May 16, 2005
Get over it: CF isn't Java
There's a lot of "We want interfaces! Nulls! Method overloading!" going on in some of the CF lists, and I really think it's a misunderstood push to become more "OO." Helping to implement CF solution in an OO manner really has nothing to do with any of the above demands, and a lot to do with shifting how you think.
Adobeflobiemedia (or whatever we'll call it this week) could implement all of the above, and it probably wouldn't make a lot of people's code any more OO. Having the constructs available (interfaces, nulls, overloading) doesn't make a language OO: just look at how much Java, C++, and C# is procedural code hammered into the OO constructs.
First: Formal, compile-time interfaces
Don't need 'em. Don't really want 'em enforced in the language.
My simple solution:
Draw a class diagram showing what the interface is, provide a shell CFC, and kick everyone's rear into following the contract. Let Macrodobe focus on cool features, not something a small percentage of CF developers will use.
Second: Nulls
They're championed because of code like this (ripping off a recent example from CFCDev):
if (person.getSpouse() == null) { write("Person is not married!") }
I really don't like this style of coding (even though it's really, really common in some languages). Instead, if a person doesn't have a spouse, and you ask for one, isn't that an exception?
try (person.getSpouce()) { catch { write("Person is not married") } }
Or, better yet:
if (!person.isMarried()) { write("Person is not married") }
Third: Overloading
He who can successfully implement overloading in a typeless language has a great big brain. Any implementation I can think of is a cheap hack that'd be endlessly debated to the harm of many and benefit of few.
I spent a good deal of my time at my last job chasing overloading hierarchies through a big inheritance tree. I really don't ever want to do it again, so please, look for more elegant ways to get things done.
PS: I'm contradicting myself a bit, as I am one of the votes for interfaces being added to the CF feature set. If possible, I'd like to retract my vote.
Mike Kelp wrote on 01/11/06 11:33 AM
I personally don't see why it is such a big deal that interface aren't implemented in CF now.I want them badly and there are many reasons why. For one thing, interfaces are very useful in API code that you expect other developers to use. When writing something for any one to use, "kick everyone's rear into following the contract" is not an option is it?
Interfaces offer us the ability to not only define some needs for an object but validate it so that a developer writing an object based off of our interface knows where the problem is when his object does not work.
I want clean heirarchies of objects that can describe themselves. I don't want my developers to have to remember how all my "pseudo interfaces" work. Interfaces could help see if in a lot of places, especially when included in all the documentation generators.
Interfaces allow us to be more flexible because one object can implement ten interfaces if it fits the bill and be very flexible. Did we forget the OO rule of programming to an interface, not implementation. Changes also come much easier, because a change can be made to an interface and any objects that depend on the interface can be found easily.
Wanting interfaces in not about asking CF to be Java. (By the way, while the language itself isn't java, it does compile into java, so the compiler/run-time benefits of interfaces are even more evident there) It is asking for a standard OO tool that could greatly benefit development in CF and usability of code written in CF if used correctly.
While I want Macromedia to implement "cool new features" as you said, I am a developer first and a "check out the cool new feature" guy second. I need to trust that I can build reliable, manageable code before I need "Cool Feature A". "Kick everyone's rear into following the contract" when a great tool that could solve the problem could be added to CF, is not an acceptable solution to me. I prefer to kick everyone's rear as little as possible and let them work effectively.
Sorry for the long post.