Local function variables and parameters in ColdFusion and Railo

Today I was doing some heavy refactoring on a project I am working for quite a long time. Moving methods, changing arguments, removing unnecessary CFCs. We are using PDF and XML merging so the project utilizes Adobe ColdFusion. Now, I am using Adobe CF for like 7 years now, I thought I know it well. While testing changes I have been stopped by an error saying "local variable X declared twice&quot. Something wasn’t right there, I just moved few methods to different components and changed few argument names. It took me good 5 minutes to figure out (even though, what I found out later, the error message was explicitly saying it) that the problem was caused bu using the same function parameter and local variable names. This was very confusing, in any other programming language I used before such an issue doesn’t exist, that was probably why I did not notice I am doing something wrong. To make sure I am right I created this sample CFC.

[source language="cf"]
<cfcomponent>
  <cffunction name="testFunc">
    <cfargument name="arg1" type="numeric" />
    <cfset var arg1 = "" />
  </cffunction>
</cfcomponent>
[/source]

And I called it in the browser. ColdFusion confirmed my assumption. My first thought was "hang on, is this a general CFML issue or just another bit that isn’t working correctly just in Adobe ColdFusion"? I started Railo and created exactly the same CFC. It appears that in Railo it is working as expected, calling the CFC with ?wsdl option returns WSDL definition.

Definitely a hint to remember.

One Response to “Local function variables and parameters in ColdFusion and Railo”

  1. Alan Pennell-Smith Says:

    Good post – this has definitely caught me out in the past so I’m a bit fastidious about my function local scoping.

    I’m sure this behaviour will be altered/fixed somehow with CF9′s built-in local scope ;)

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>