Duck Typing Lite
My background is not in Java or C++. My first steps into real programming was ColdFusion and I later learned those other languages (and many more). This has left a conceptual hole in my education. This has led to me being confused by some of the writings that people in the ColdFusion community are posting about Duck Typing.
I've read article after article on it and there seems to me to be no clear definition. There are concepts mixed into most articles that seem to have nothing to do with Duck Typing like Mixins and Interfaces. There are concepts thrown around that seem to muddy the understanding of what should be a simple idea. What follows will be my understanding of Duck Typing with a concrete example. Unfortunately, this understanding is neither perfect nor what Duck Typing is according to those in the know. This is why I refer to it as Duck Typing Lite.
Duck Typing is a conceptual approach to creating CFC methods where the method does NOT use the CFFUNCTION or CFARGUMENT tags to try and validate the data being passed in or out. Instead, the focus is put on the value of the data and if it 'works' for the method. To explain it better, lets look at a UserInfo method. This method will accept an identifier for a user. This can be the users ID number or their user name. Both are valid 'messages' that will be sent to the method and the method will decide how to deal with it.
<cfargument name="UserID" required="yes">
<CFSET var qUserInfo="">
<CFIF IsNumeric(Arguments.UserID)>
Run query to get user info based on numeric userid
<CFIF qUserInfo.RecordCount>
<CFRETURN qUserInfo>
<CFELSE>
<CFRETURN "no user for this user id">
</CFIF>
<CFELSEIF IsSimpleValue(Arguments.UserID)>
Run query to get user info based on numeric userid
<CFIF qUserInfo.RecordCount>
<CFRETURN qUserInfo>
<CFELSE>
<CFRETURN "no user for this user name">
</CFIF>
<CFELSE>
<CFRETURN "Invalid user info message passed">
</CFIF>
</cffunction>
So basically duck typing is where a message is sent to a method, the method deals with the message and then returns a message to the calling template. What makes it different from what your doing now is that there is no validation of the data 'types' being passed in or out. Focus is on the data being passed, not what type it is.
This was my understanding of Duck Typing and I was told I'm on the right track, but not exactly. Problem is, the example I was given still has me confused as to the specifics. As soon as I have a clean and clear understanding of the subject, I'll post it here.


I would take the route of throwing an exception from your method and letting the calling code decide what to do in the event of an exception. Both approaches rely on the calling code deciding what to do when the expected result isn't returned, but the exception route keeps the API of your method from being variable. My 2 cents of course. ;-)
Throwing an error on an error rather than returning a different data type makes total sense and was my first thought. Keeping the ability to pass in variables without checking them so as to use them based on their content does 'feel' good though.
It's still a work in progress as I learn more of the concept. The real issue is that for Ruby, all variables are classes. This is not true for CF so refering to a passed 'thing' as a class will only work if it actually IS a class (i.e. a CFC reference).