If you pay attention to the ColdFusion blogs at all, or if you were at CFUnited, you're well aware of the new-found duck typing craze in ColdFusion land. While I agree with some of the sentiments behind advocating duck typing, as with most things that become a veritable feeding frenzy, there's quite a bit being lost in the insanity in my opinion.
First and foremost, just because you can do something doesn't mean you should. Let me state that again and extend it a bit. Just because you can do something doesn't mean you should, and it certainly doesn't mean it's appropriate to do it all the time. My fear with all the blog postings I've been reading the past few days is that people will take on an attitude along the lines of, "Typing? Who needs it! This is ColdFusion!" Personally I think that's exactly the wrong approach.
Let's stop and think about why typing exists in strongly typed languages such as Java. Yes, I know ColdFusion isn't Java, but kindly stick with me for a moment. Typing exists in Java for numerous reasons, not the least of which is that type checking makes for safer software. Why? Because in Java if you're doing something wrong, either on purpose or inadvertently, the compiler won't let you get away with it. If an object's expecting a Person to be passed in as an argument and you try to pass it a ShoppingCart, that simply isn't going to fly. No worries about weird runtime errors based on typing because you can't even get that far. From my years as a Java programmer I'll say this much: once you get the darn thing compiled, you've won well more than half the battle. Java feels solid precisely because of this aspect of the language, and many other languages both before and after are strongly typed for many of these same reasons.
Is dealing with strong typing a bit of a headache? Sure, particularly when you're in a situation where duck typing is actually necessary. Re-read that last part. "... where duck typing is actually necessary." Now given the fact that there isn't, strictly speaking, a way to even do duck typing in Java, and there are plenty of Java applications out there running just fine without it, "necessary" might be a bit strong in this case. So let's downgrade that to "... where duck typing sure would come in darn handy and I'm doing it because I know what I'm doing and actually mean to remove type-checking and all the benefits that go with it." One of the ways Java addresses cases in which duck typing would come in handy is to use interfaces, which--and the discussion of interfaces is a topic for another blog post altogether--ColdFusion doesn't have. (As an aside, BlueDragon 7 adds a cfinterface tag, and it was rumored at cf.objective() that CF 8 may add interfaces as well.)
Now back to ColdFusion. As many have pointed out over the past few days, ColdFusion is not Java. Fair enough. What I take issue with is the extension some folks seem to be applying to this statement. Specifically I find statements such as "ColdFusion isn't Java, so don't worry about typing at all" extremely problematic. ESPECIALLY if people are new to OO and they're bypassing the proper learning of OO and jumping straight to going typeless everywhere in all their applications, this is utter madness.
Granted, because all the type checking in ColdFusion is done at runtime, we don't get the same compile-time checking that Java developers enjoy. (Take "enjoy" with a grain of salt there; the Java compiler is a rigid taskmaster yet we are forced to seek its approval and are generally elated when we receive such approval via a successful compilation.) That doesn't, however, completely negate the value of typing in ColdFusion.
First let's think about the development process. ColdFusion checks type at runtime, but during development, when you change a file and fire things off again, behind the scenes things are getting compiled and subsequently they are immediately executed. So this is really a compilation step with automatic execution thereafter. Not so different. If you're passing something an object of a type it's not expecting, ColdFusion throws an error. During development you may absolutely want this to happen, and it can save you some headaches because you get no further than CF saying, "Hey pal, wrong object type. Try again." This can be great for debugging purposes.
Next, let's think about deploying an application to production. Do you care about typing once the application is complete, has been thoroughly debugged, and you're absolutely, positively sure everything's going to work correctly? Probably not. You don't really care about it at that point in Java either. Once all the bugs have been ironed out you know the correct types are being passed, so conceptually at least, it probably no longer matters. Of course there are special cases where it might matter, such as if you're writing an API and people interact with your API by throwing objects at it, a good old-fashioned "wrong object type" error might be better than something that could be much harder to debug.
The other advantage of duck typing people seem to be going nuts over is performance. In Java, type checking is done at compile time so even if you could do straight duck typing in Java, there's no performance benefit. In ColdFusion on the other hand, type checking is done at runtime, meaning this is an additional step ColdFusion has to perform. No huge surprise then that if you don't make ColdFusion perform this additional step, things are going to be faster. How much faster? Depends on the application. My real concern here is performance gains ... at what cost? Duck typing for the sake of performance should only be done in very specialized circumstances, and I'll probably have more to say on this point soon.
Before I ramble further, I'll cut to the chase and summarize by saying that duck typing is darn handy when you need it, but eliminating typing altogether because it's the new, hip thing to do, or because you think it's the top-secret weapon that will make your CF apps lightning fast, is a big, big mistake. If you listen to ColdFusion Weekly you'll probably recall the call we had a couple of weeks ago asking for our thoughts on the Hal Helms and Jeff Peters Out Loud episode where they discussed "GroupThink." Duck typing is another case in point in my mind. Please, please, please, for the love of the OO gods, think about what you're doing when it comes to duck typing. Don't skip typing because all the cool kids are doing it.
So where do I think this is all going? I hope that we're in another Hegelian phase of our collective development knowledge. Thesis: ColdFusion has a type attribute now, so use it. Antithesis: forget typing--duck typing all the way. Synthesis: use duck typing only when you need it. We're currently still in the antithesis phase which is why I thought it was a good time to do my own personal brain dump on this topic.
I'll conclude with a few bullet points that you can use as food for thought.
- Duck typing is great when you need it, but in the vast majority of cases you care about an object's type (at least philosophically).
- Duck typing for performance is a very bad reason to do duck typing, except in specialized circumstances.
- Duck typing throughout your application can make development and debugging much more of a hassle.
- Just because ColdFusion checks type at runtime doesn't entirely negate the benefits of typing.
- OO is to a large degree about discipline. Don't duck type at all until you've done it the "right" way for quite a while.
- Learn Java. I'm not saying learn it to the point of expertise, but play around with Java and learn how Java thinks. You have to know the rules before you can intelligently (and hopefully judiciously) break them.
- Make up your own mind. Don't duck type until it makes sense to you to duck type, and even then, only do it when you need it. (Yes, I know I repeated that point, but it's and important one.)
That's all my quacking (har har har) on duck typing for now.
http://www.robgonda.com/blog/index.cfm/2006/7/4/PostCFUN-Must-read-linksI'd personally recomment Vy a Duck (and getting a subscription to Fusion Authority Quarterly Update - it looks like being quite a magazine - especially with articles on cool stuff like flash forms :->).Also, as a practical matter, unless you've had a problem with the lack of interfaces or nulls in CF, you probably don't need to worry about duck typing. It's like any design pattern - very hard to understand until you come across the forces that drive it in your latest app. And then it becomes an obvious solution to your real world problem!Best Wishes,
This is the best posting on the subject I've read so far (and, yes, have read quite a number of them in the last few days).Good work.--
http://en.wikipedia.org/wiki/Duck_typingJudith Dinowitz also has an excellent article on Fusion Authority:
http://www.fusionauthority.com/Techniques/Article.cfm/ArticleID:4588That should help put the discussion in context.As for the question on interfaces, I'll try to post again on that topic at some point, but again wikipedia might help in the mean time: