Coldfusion


Quick and dirty fix for missing coldfusion arguments

While chasing down a bug in coldfusion code today, I came across the following code. I’ve altered the function names for privacy reasons, but you’ll get the gist of it. For the record, I’m not proud of this code, but it minimized my coldfusion pain because it was quick.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!-- invoke myFunction -->
<cfinvoke method="myFunction">
	<cfinvokeargument name="customerStoreId" value="">
</cfinvoke>
 
<!-- pass customerStoreId to printCustomerStoreId function -->
<cffunction name="myFunction">
	<cfargument name="customerStoreId" type="string" required="no">
		<cfinvoke method="returnCustomerStoreId">
			<cfinvokeargument name="customerStoreId" value="#customerStoreId#">
		</cfinvoke>
</cffunction>
 
<!-- print customerStoreId -->
<cffunction name="returnCustomerStoreId">
	<cfargument name="customerStoreId" type="any" required="yes">
		<cfoutput>#customerStoreId#</cfoutput>
	<cfreturn #customerStoreId#>
</cffunction>

A few things to note with this code:
1. Both functions have an argument “customerId”, but the function called first, myFunction, specified the customerid argument as required=”no” while returnCustomerId, which is called from myFunction, specified the required attribute as required=”yes”. Which is it?
2. No default value was provided for the optional argument in myFunction.
3. The argument type for customerId in myFunction is “string” while the type is “any” in the returnCustomerId function. Actually, both should specify “numeric” as the argument type.

Solution 1
Backtrace as far back as possible and make sure the customerId is set before myFunction is invoked. This function is called from a few different places in the code base and a lot of testing would be required to ensure the bug was fixed. Total time required: 2-3 hours.

Solution 2
In our case, we want to spend as little time as possible debugging and fixing coldfusion code since since it’s being phased out and we’ve switched to PHP. The quick solution was to alter the arguments by setting the type to numeric on printCustomerStoreId, required to yes, and simply including an if statement in myFunction that will set the customerStoreId to 1 if an empty string is passed in. Unfortunately, setting a default value won’t help since the variable does exist in the functions, even though it was empty string. Am I proud of this code? No. I can’t even figure out why I’m posting it. Total time required: 6 minutes (less time than it took to write this).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!-- invoke myFunction -->
<cfinvoke method="myFunction">
	<cfinvokeargument name="customerStoreId" value="">
</cfinvoke>
 
<!-- pass customerStoreId to printCustomerId function -->
<cffunction name="myFunction">
	<cfargument name="customerStoreId" type="any" required="yes">
		<cfif customerStoreId eq "">
			<cfset customerStoreId = 1>
		</cfif>
		<cfinvoke method="printCustomerStoreId">
			<cfinvokeargument name="customerStoreId" value="#customerStoreId#">
		</cfinvoke>
</cffunction>
 
<!-- print customerStoreId -->
<cffunction name="printCustomerStoreId">
	<cfargument name="customerStoreId" type="numeric" required="yes">
		<cfoutput>#customerStoreId#</cfoutput>
</cffunction>