By Peter Bell

Duck Typing and Documentation

Adam Haskell has just posted on an interesting approach to duck typing that he mentioned in Sean's talk on the topic yesterday morning.

To briefly summarize, duck typing is an approach to taking advantage of the fact that ColdFusion (like Ruby) is not statically typed. There are many potential benefits (and pitfalls for the unwary) of this approach. One of the potential benefits is that by removing the overhead of asking ColdFusion to check the types at runtime, performance critical parts of your application could be speeded up.

The problem with this is that if you are only removing types to get the performance boost (there are other use cases for duck typing I'll blog about later), you are left with code that is less readable and less well documented. Sean takes care of this in the hints - probably something like: hint="I am the user name: string. Required". What Adam has done is write a Regex that removes the performance degrading code as part of his build script, allowing for the benefits of documentation along with the potential performance enhancement of duck typing.

Two caveats: 1. The performance benefits may vary (and may or may not be relevant - please - no premature optimization). 2. There are other interesting uses for duck typing. Please don't think it is JUST about the speed.

Comments
Peter, I know I'm supposed to come here and tell you that you're full of crap, but I'll have to keep looking here.

I never heard of "duck typing" before CFUNITED-06. People keep investing buzzwords for stuff I do. Well I don't do a lot of it. The fact is, almost every app I've ever built doesn't really need to be performance optimized. Even if someone famous started blogging use my blog app, I don't think that would really ever *NEED* the kind of performance enhancements that could (theoretically) be achieved by ducktyping.

I do it more on the return type than the argument types of a CFC. Mostly because I sometimes just want my CFC to return false if it didn't work. Or return some data structure or something if it DOES work. I didn't actually do anything like that until I started doing ajax development. You could look at the code and say "Why the hell did you do that when you could've done this and strongly typed your return value" but sometimes in the javascript ajax code it's just easier to deal with booleans!

So ask yourself....

#1 - Is the alleged performance improvement a worthwhile gain to having code that doesn't self-document as well... and
#2 - will your app ever really benefit from the alleged performance improvements... and..
#3 - if so, how hard would it be to remove the offending strict typing code LATER should the issue arise.

Personally, I choose self-documenting code rather than duck typing just for the sake of performance. every cffunction I write in every app I write *ALWAYS* has access, output, and returntype arguments.

(ack, I hate captchas! I screwed up the first one!)
# Posted By Rick Root | 7/1/06 1:18 AM
Hi Rick,

Thanks for the comment! Sean was careful to point out that this is only something to do on parts of your application that are showing up under load testing to be a problem in terms of performance. Also, with Adam's Regex/ant build script, you keep the strict typing in your code - it just gets stripped out before it is passed to ColdFusion so you get the best of both worlds.

As you pointed out though, there are other uses for duck typing - one of which is to handle the fact that we don't have a null in ColdFusion.

Best Wishes,
Peter
# Posted By Peter Bell | 7/1/06 2:07 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.005.