Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
I am the chief technical officer at InVision App, Inc - a prototyping and collaboration platform for designers, built by designers. I also rock out in JavaScript and ColdFusion 24x7.
Meanwhile on Twitter
Loading latest tweet...
Ben Nadel at cf.Objective() 2013 (Bloomington, MN) with: Ryan Anklam

Passing A Variable Number Of "Nameless" Arguments Using CFInvoke And ArgumentCollection

By Ben Nadel on
Tags: ColdFusion

I have been trying to figure out how to send a variable, logic-based selection of arguments as unnamed values to a function. My original thought was to use ColdFusion's CFInvoke and CFInvokeArgument, but CFInvokeArgument requires a name attribute (which is what I was trying to avoid). I couldn't just call the function using function notation (ex. "Test()") as I needed to involve logic in my argument selection (which only CFInvoke would allow).

Sean Corfield had a good idea to use the index-like keys. But for some reason, it didn't click in my head until Christoph Schmitz suggested the same thing. While both suggested this solution, I think it only dawned on me "how" to accomplish this when Chris said it (right place, right time). Both are clearly rock stars though.

So anyway, here is my test to see that it works. These are two functions that I tested with. The first, DumpArgs() is the ColdFusion UDF that just dumps out its arguments. The second function, StructCreate() is just a utility function (seen all over the place) that builds a struct on the fly and returns it:

  • <cffunction
  • name="DumpArgs"
  • access="public"
  • returntype="void"
  • output="true"
  • hint="Does nothing but CFDump out the arguments.">
  •  
  • <!--- Dump out arguments. --->
  • <cfdump
  • var="#ARGUMENTS#"
  • label="ARGUMENTS Scope"
  • />
  •  
  • <!--- Return out. --->
  • <cfreturn />
  • </cffunction>
  •  
  •  
  • <cffunction
  • name="StructCreate"
  • access="public"
  • returntype="struct"
  • output="false"
  • hint="Returns a created struct based on the passed in key-value pairs.">
  •  
  • <!--- Define the local scope. --->
  • <cfset var LOCAL = StructNew() />
  •  
  • <!--- Create the return struct. --->
  • <cfset LOCAL.Struct = StructNew() />
  •  
  • <!---
  • Populate the struct using the name-value pairs
  • of passed in arguments.
  • --->
  • <cfloop
  • item="LOCAL.Key"
  • collection="#ARGUMENTS#">
  •  
  • <cfset LOCAL.Struct[ LOCAL.Key ] = ARGUMENTS[ LOCAL.Key ] />
  • </cfloop>
  •  
  • <!--- Return struct. --->
  • <cfreturn LOCAL.Struct />
  • </cffunction>

Ok, now I need to create some values that I may OR may not need to pass in. It's friday, let's build a struct of hot girls that we can choose from:

  • <!--- Create a struct of girls. --->
  • <cfset objGirls = StructNew() />
  •  
  • <!--- Create girl. --->
  • <cfset objGirls[ "Samantha" ] = StructCreate(
  • Name = "Samantha",
  • Hair = "Brunette",
  • Hotness = 9.0
  • ) />
  •  
  • <!--- Create girl. --->
  • <cfset objGirls[ "Niki" ] = StructCreate(
  • Name = "Niki",
  • Hair = "Brunette",
  • Hotness = 8.0
  • ) />
  •  
  • <!--- Create girl. --->
  • <cfset objGirls[ "Lori" ] = StructCreate(
  • Name = "Lori",
  • Hair = "Blonde",
  • Hotness = 7.0
  • ) />
  •  
  • <!--- Create girl. --->
  • <cfset objGirls[ "Tamoko" ] = StructCreate(
  • Name = "Tamoko",
  • Hair = "Black",
  • Hotness = 8.0
  • ) />
  •  
  • <!--- Create girl. --->
  • <cfset objGirls[ "Ashley" ] = StructCreate(
  • Name = "Ashley",
  • Hair = "Brunette",
  • Hotness = 8.0
  • ) />
  •  
  • <!--- Create girl. --->
  • <cfset objGirls[ "Libby" ] = StructCreate(
  • Name = "Libby",
  • Hair = "Brunette",
  • Hotness = 9.0
  • ) />
  •  
  • <!--- Create girl. --->
  • <cfset objGirls[ "Christina" ] = StructCreate(
  • Name = "Christina",
  • Hair = "Blonde",
  • Hotness = 9.0
  • ) />

Now that we have a our function in place and our possible values, let's create a variable-length argument collection based on some business logic:

  • <!---
  • Create an argument collection of girls. The
  • purpose of this is to build up a variable number
  • of "nameless" arguments.
  • --->
  • <cfset objArgs = StructNew() />
  •  
  • <!---
  • Loop over the girls to figure out which ones we
  • want to pass in. For now, let's get all the 9.0
  • who are also brunette.
  • --->
  • <cfloop
  • item="strGirl"
  • collection="#objGirls#">
  •  
  • <!--- Get a reference to the girl. --->
  • <cfset objGirl = objGirls[ strGirl ] />
  •  
  • <!--- Check to see if this girl makes the cut. --->
  • <cfif (
  • (objGirl.Hair EQ "Brunette") AND
  • (objGirl.Hotness EQ 9.0)
  • )>
  •  
  • <!---
  • We want to include this girl in our argument
  • collection. In order to do that we are going to
  • add her to the argument collection struct with an
  • index-based naming convention so that the function
  • will use it like an array. In order to do this
  • dynamically, we can use the struct-count to get
  • the current index.
  • --->
  • <cfset objArgs[ StructCount( objArgs ) + 1 ] = objGirl />
  •  
  • </cfif>
  •  
  • </cfloop>
  •  
  •  
  • <!---
  • Now that we have built up the argument
  • collection, we can simply invoke the method and pass
  • in the arguments struct.
  • --->
  • <cfinvoke
  • method="DumpArgs"
  • argumentcollection="#objArgs#"
  • />

This gives us the following CFDump:


 
 
 

 
Variable Length Argument Collection CFDump  
 
 
 

That works perfectly! I think the thing that finally clicked in my head when Chris suggested the argumentCollection was how to build the struct without having to use an incrementing value (ie. keeping some sort of argument count variable). By using the StructCount() I was able to just keep appending values to the struct and not having to worry about what index I was actually working on. Sweet!

Note: Using argumentCollection can also be done with standard function invokation. It does not require the use of CFInvoke.




Reader Comments

FWIW, #dumpargs(argumentcollection=objArgs)# would work just as well as <cfinvoke method="DumpArgs" argumentcollection="#objArgs#" />

Reply to this Comment

@Steve,

The PPT looks cool. I will take try to take a look at the other files this weekend.

@Matt,

Yeah, good point. I was just using CFInvoke cause that's how I started out. But you are absolutely right.

Reply to this Comment

@Steve,

I finally got around to going through your CFC framework. It looks pretty interesting. I see what you are saying about passing around all of your arguments like this; you are continually passing your ARGUMENTS scope onto other methods.

I don't know enough MVC / Fusebox to comment on the functionality, but it does look very clean. I am sure it will positively influence my future thought processes.

Reply to this Comment

Hi Ben,

I am trying to do this, but in my cases I want the arguments to be passed in a specific order. The argumentcollection trick does not work in this case, as the elements inside a struct have no specific order. Any cool ideas for that?

Greetings from the Netherlands
Martijn

By the way I loved your preso about the CF application framework for CFUG-NL!

Reply to this Comment

@Martijn,

Very interesting! I think you uncovered a difference that I was not aware of. It seems that argumentCollection has a different behavior in CFInvoke than it does have in a standard method invocation. If you switch to using CFInvoke, this should work.

Thanks for the insight!

Reply to this Comment

Hi Ben,

Thanks for replying. I reckon you mean I should switch to CFInvokeArguments, instead of passing an argumentcollection? Because passing in an argumentcollection would mean passing a struct, inside which the elements can never be trusted to have any specific order, right?

Reply to this Comment

If you need to pass unnamed arguments to a function, create the arguments in an array and then loop over the array passing them via numbered <cfinvokeargument> tags to the function.

Here's a variation of your own code from a different blog post:

<cfset args = []>
<cfset args[1] = "Katie (1st Arg)">
<cfset args[2] = "Colleen (2nd Arg)">
<cfset args[3] = "nameless third arg">

<cffunction name="echoArguments">
<cfargument name="namedArg1">
<cfargument name="namedArg2">
<cfreturn arguments>
</cffunction>

<cfinvoke method="echoArguments" returnvariable="result">
<cfloop index="i" from="1" to="#arrayLen(args)#">
<cfinvokeargument name="#i#" value="#args[i]#">
</cfloop>
</cfinvoke>

<cfdump var="#result#">

I don't see a way of passing an array of arguments to a function using standard function-passing notation though.

I guess depending on one's perspective, this is either a benefit of tag-based code, or a shortcoming of normal code. It'd be solved by allowing an argumentCollection to be an array though, that's for sure. Maybe for CF10?

--
Adam

Reply to this Comment

@Adam,

Now this is funny - this CFInvokeArgument approach is actually part of the idea that Sean had originally told me about; and, when I saw it, I think I simply equated the set of CFInvokeArgument tags to be the same, functionally speaking, as argumentCollection. I think this is where the conversation in the other blog posts comes from.

I guess it would appear that CFInvokeArgument is not functionally the same as argumentCollection.

Reply to this Comment

@Adam,

I just ran some CFInvokeArgument-based code in CF9 and what I'm seeing is that CFInvokeArgument will properly map 1/2 to args1/args2, but *only* if the target method has named arguments defined. This appears to act like the argumentCollection as well.

Bottom line, CF9 simply makes passing arbitrary non-named arguments to a method a non-trivial task. Looks like an evaluate() approach for N-ordered arguments might be the only approach.

I agree with you - being able to use an array for argumentCollection would be awesome :)

Reply to this Comment

Post A Comment

You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.