Liking using implicit arguments for ColdFusion 9 functions

ColdFusion 9

Each new version of a computer language inevitably brings about changes, mostly for the better, in the style of writing that language.

One change I have enjoyed with ColdFusion 9 is using implicit structures as arguments to a function.  This leads to bundling of arguments into structures in cases where it makes more sense.  Granted this has been going on in languages like JavaScript (and even the ColdFusion Javascript functions!) for a long time.  There are definitely good and bad reasons to use this style.

Lets take a function, written with arguments individually defined, and see how this works:

function getAndCache( url, cacheType, cacheTime, cacheContent )

That can be rewritten to:

function getAndCache( url, cache )

This time we put all the cache settings in the cache argument.

Advantages:

  • All cache settings are put in one structure.
  • These arguments can potentially be defined in one application wide place and passed in easily (in a similar way that argumentCollection can be used).

Disadvantages:

  • Slightly harder to document and validate

As I mentioned earlier I'm not suggesting using this for every function but for those that have a collection of config or style options it can work well.  This is seen in a lot of JavaScript functions and used widely by jQuery.

Steve said:
 
It can be cool and useful. When I do this I try and make sure my hints are clear and accurate on my functions to enable clearer doc generation of my CFCs.
Lately, I've liked using this approach in Ajax applications where, using CFAjaxProxy, I've been passing Javascript JSON objects to my CF functions via the proxy. The functions accept an argument of type Struct (the JSON object), containing multiple values. The combination of all this has made making CF/Ajax apps a breeze! Especially for prototyping. Cheers.
 
posted 142 days ago
Add Comment Reply to: this comment OR this thread
 
Don said:
 
I use this approach a lot when there are many default options. In jQuery I have object of defaults and extend the input object with the defaults. In CF I use cfparams for all the expected values.

The downside is that you don't have an explicit contract with the consumer of the function so other developers better understand it. The upsides are that the consumer only has to specify the optional values he wants to set and that you get away from positional arguments which are a real pain when you have lots of options.
 
posted 142 days ago
Add Comment Reply to: this comment OR this thread
 
Andy Sandefer said:
 
I'm with Steve on this as far as using the hint attribute. I got lazy about using this in my cfcs for a few years but lately I've rediscovered the importance of more than just in line commenting.
 
posted 142 days ago
Add Comment Reply to: this comment OR this thread
 
samfarmer said:
 
Thanks for the comments all. Using hint for comments is definitely the way go and something I, um, do most of the time! It is a little harder to do with script based components and functions though.
 
posted 142 days ago
Add Comment Reply to: this comment OR this thread
 

Search

Twitter
You should follow me on Twitter here
About Me
I am a 34-year old Web Developer specializing in ColdFusion. I live and work in downtown Washington, DC with my wife and two daughters. Read more About Me

2007 CFeMmy Best Newcommer winner
As voted on by fellow CF Bloggers.