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.
2
3 /**
4 * Function to duplicate the <cfparam> tag within CFSCRIPT.
5 * I attempted to make cfparam faster by eliminating
6 * the evaluate, and using structkeyexists
7 * According to my tests this is MULTUPLE times faster
8 * than the cfparam tag when the variables
9 * 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:
2
3 foo = structparam(attributes,'bar','default value');
4
5 </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.

http://www.communitymx.com/content/article.cfm?cid...
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.
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))
)#
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.