Passing Functions as Arguments, Who Knew?
It's good to learn something new every day. You can always do better. You can always learn more. Today I learned something that I suppose should have been obvious to me: given that ColdFusion is a weakly-typed, dynamic scripting language, you can pass functions as arguments to other functions.
This can be really useful for custom renderers: say you have a series of products but each need to be output in a slightly different way. You could write separate functions with a lot of repeated code to handle the rendering of each of the products, or you could be a good developer and pull out the repeated code in to a single function. You could be an even better developer and pass in a custom rendering function to the generic rendering function to handle the unique processing that each product needs for proper rendering. The generic function then calls the custom function that you passed in as an argument. This leads to simpler syntax, more flexibility, and reuse of functionality. Harel has an example of passing in a function as an argument on his blog.


Depending on the circumstances I think I would tend to use polymorphism for handling the different product types, like say, if I were working on a site that allowed for the sale of diverse items like eBay or CraigsList where the adds range from ipods to cars to real estate.
Although where using the first-class function has been real handy for me personally is in developing the XHTML abstraction library for the onTap framework where the majority of elements use the same display code, but can have a function embedded to override the default behavior. In that case it's not much different than polymorphism though, it's just that the html elements are structs instead of CFCs (primarily for performance reasons).
The place where the technique really shows its usefulness imo is in a ternary sorting function (where polymorphism isn't really as applicable or useful). You pass in an array and a function as arguments and it loops over and sorts the array based on the results of the ternary function against each pair of array elements. James Sleeman contributed a QuickSort function to cflib using this technique a long time ago. I found it so useful I included it in the onTap framework's util library back when it was still CF5. http://cflib.org/udf/QuickSort