Duck Typing Dichotomy
So it seems after attending the CFUnited 2006 conference that yet another feud is brewing in the Coldfusion community. The all eternal question is arising – to duck type or not to duck type. I can see it now, next year we will have the Duck Type Celebrity Death Match between Sean Corfield and Hal Helms against……well…….no one yet……..hey…….maybe that Simon guy will throw on the gloves again.
For those of you sitting at home going quackers maybe I can help explain.
Duck typing is the basic concept of not typing CF functions and function arguments. This simply means you leave off the ‘returntype’ and ‘type’ attributes in cffunction and cfargument tags, respectively. So why would someone want to do that….I’m glad you asked.
Let’s pretend you have two different objects, a car object and a plane object. These objects are clearly different and cannot really be subclassed (one cannot extend the other by nature). However, they may have many similar functions. Let’s suppose they both have high level functions that passengers may want to call, namely, GetIn(), BuckleSeatBelt(), and EnjoyRide(). Although the functions have the same names the details are quite different. For instance, getting into a car means opening the door and stepping in. Getting into a plane involves climbing stairs or walking through the tunnel to the aircraft. The same could be said for EnjoyRide(), in a car you might listen to music but in a plane you might want to order a Pepsi. Clearly we have two objects with the same function names but the code in the functions are different.
Now, let’s say we have a Transportation() function that calls the three functions GetIn(), BuckleSeatBelt(), EnjoyRide(). We will pass it the mode of transportation and let it call the functions for us. If we type the Transportation() function argument we must provide the cfargument type=Car or type=Plane but we cannot define both. Therefore we might create two functions, one that accepts cars and one that accepts planes. This would be the method of choice in language that supports true polymorphism (two identically named functions that take different argument types), however, CF does not support this type of polymorphism. So the next best thing is to take away the type attribute of the cfargument tag. That way either object can be passed in and have their functions invoked.
There are other scenarios where duck typing is essential. Since Coldfusion is in the minority of languages that actually allows you to do it we should definitely take advantage. However, there are downsides. For instance, what if you are working on a large development project and you spot another developers function that you want to use. It requires 5 parameters but displays no type attribute information. How do you know what to pass that function? Well, either you hope the developer left a nice comment for you (and we know how common that is) or you can look through his code (ok you can stop laughing now). Neither solution is good.
Another point to throw into the mix is that it takes time for CF to type check at runtime. If you have a function that is continually called with multiple paramters that contain types then CF must check those types everytime before the function is run. Taking out the types can speed up the run time of the application.
My advice would be to use typing when you are developing an API that will be used by other developers, however, you should then strip out the typing information as part of your build process. This will allow you to keep typing information on the development environment but not in production when performance counts. If you are not developing API code, or you are the only person that will be working with the code, or you need duck typing to tackle a particular problem then take out the typing information in development as well as production.
That should sum up the issue, shoot me a comment on your thoughts.