My cfscript version of cfparam

Some time ago while working in cfscript I had the need to cfparam a bunch of variables. I was building an XML document out of form fields and a number of the fields wouldn't always exist. At first I made a simple UDF to encapsulate a cfparam tag, but it this code was inside of a loop that beat it over and over again before it was finished and performance just wasn't cutting it. The culprit as it seemed was scope hunting.

Before I go too far, I would like to point out there have been many long-winded discussions about performance, what code runs better on what version of CF, and what a proper load test is etc. That is really neither here nor there. I am simply documenting how I solved my problem.

I changed the function to use isdefined() and first, but then still unhappy with the apparent scope hunting and ambiguity I switched to using structkeyexists() and structinsert() and I called the function structparam(). The idea was to be able to set the default value AND get the value out at the same time.

I believe the reason I had so much better performance out of it was because in my specific scenario a majority of my variables were most likely NOT to exist. Since I pass the scope/struct into the function I believe that effectively eliminated the need for CF to search all the scopes.

This was what I came up with.

 <cfscript>
 
 /**
  * Function to duplicate the <cfparam> tag within CFSCRIPT.
  * I attempted to make cfparam faster by eliminating
  * the evaluate, and using structkeyexists
  * According to my tests this is MULTUPLE times faster
  * than the cfparam tag when the variables
  * being paramed are more likley NOT to exist.
10   *
11   * @param struct      The struct.
12   * @param key      The name of the key
13   * @param default_value    The default value. If not passed, use
14   * @return Returns the value of the variable parammed.
15   * @author Brad Wood
16   * @version 1, November 16, 2006
17   */

18  function structparam(struct,key,default_value) {
19  
20      if(not structkeyexists(struct,key))
21          {
22              structinsert(struct,key,default_value);
23              return default_value;
24          }
25      else
26          {
27              return structfind(struct,key);
28          }
29  }
30  
31  
</cfscript>

Usage was as follows:

 <cfscript>
 
 foo = structparam(attributes,'bar','default value');
 
 
</cfscript>

I really wish ColdFusion's structinsert() function did NOT an throw an error when "allow overwrite" is set to no and the key exists. Then it would be the perfect replacement for a cfscript param.

Related Blog Entries

TweetBacks
Comments
# Posted By dave | 7/19/08 11:40 PM
Don Q's Gravatar Tom Muck's article uses IsDefined. Cover's UDF is much more efficient I would imagine.

And While looking at Tom Muck's cfparam UDF, I'm still having a hard time finding where the second argument of "default" is used.
# Posted By Don Q | 7/20/08 12:08 AM
Brad Wood's Gravatar Yeah, I had trouble following Tom's param function. It does appear to ignore the default value. His function also returns a string, yet his sample usage does not use the returned value.
# Posted By Brad Wood | 7/20/08 1:10 AM
Don Q's Gravatar Yeah, noticed that too, but I guess on his example instead of just cfparam(var,param); he could use it as var = cfparam(var,param);

But, I've been thinking. Cant we use something from the GetPageContext? Below is just conceptual, far from working.

<cfset new={random="000"} />
#iif(
   len(GetPageContext().SymTab_findSimpleName('new.random')),
   GetPageContext().SymTab_findSimpleName('new.random'),
   GetPageContext().SymTab_setSimpleName('new.random',RandRange(1,999))
)#
# Posted By Don Q | 7/20/08 1:29 AM
dave's Gravatar i had troubles with tom's as well.
I did see on ben forta site about the new cf committee looking into adding more tags into cfscript & hopefully this will be one of them.
# Posted By dave | 7/20/08 5:37 PM


BlogCFC was created by Raymond Camden. This blog is running version 5.9.5. Contact Blog Owner