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.

<CFFUnction name="UserInfo" access="public" output="false">
   <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>
This method will either return a query 'object' (data collection) to the calling template or a string with an error message. It is up to the calling template to decide what to do with the 'message' being returned from the method.
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.

Comments
Brian Kotek's Gravatar Michael, I don't like this, and here's why: the method can return either a query or a string. With duck typing, it's one thing to return one of several different CFC types, becuase they're all still CFCs. The only difference is the messages those CFCs can respond to. The approach you're showing is substituting an entirely different type (query vs. string). This complicates the API unnecessarily.

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. ;-)
# Posted By Brian Kotek | 2/8/06 1:22 PM
Michael Dinowitz's Gravatar My initial example was based on my incomplete knowledge of duck typing. What I had in my head at the time was that duck typing does not check types going in or out which allows for anything in or out. As I now have a clearer understanding of duck typing as described by Hal, I'm going to redo this idea and keep it as duck typing lite.

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).
# Posted By Michael Dinowitz | 2/8/06 2:20 PM
Microsoft Office's Gravatar in different folders for easy accessibility.
# Posted By Microsoft Office | 12/13/11 3:44 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.9. Contact Blog Owner
House of Fusion | ColdFusion Jobs @ House of Fusion | Fusion Authority