One More Good Reason to Define Local Variables in Function Instead of Variables Scope

I used to create application variables for my cfc object which usually called many times from my coldfusion pages. This will help you to save execution time required to create object everytime. But it is highly recommand that you use local scope for all function local variables or there will be change your variables scope value may overwrite by other function. As a best practice it is always advisable to use local scope for function but by nature of programmer we always avoid to use it specially need faster development. Again this is not GOOD.

I have created sample example which gives you better understanding between local scope and Variables scope in function. I have one CFC and two CFM page which called CFC object.

 

scrible.cfc
One function having one argument and loop through it to consume some time and at last return i (index of loop) which should be arg1 + 1. Please note that index variable ‘i’ not defined in local scope so it will consider under variables scope.

[code:cf]
<cfcomponent output="false">
<cffunction name="testMe" access="public" output="false" returntype="any">
<cfargument name="arg1" required="true" />
<!— Loop to consume some time. —>
<cfloop from="1" to="#arguments.arg1#" index="i">
</cfloop>
<cfreturn i>
</cffunction>
</cfcomponent>

[/code]

 

scribble.cfm

Simply create object of component scribble.cfc and store in application variable so I doesn’t need to create everytime. Generally I used to create objects in Application.cfc’s OnApplicationStart function. Called function testMe from component and print return value.

[code:cf]<cfset m = 0>
<cfset application.mycfc = createObject("component","scribble")>
<cfset m = application.mycfc.testMe(100000000)>
<cfoutput>#m#</cfoutput>[/code]

Expected Output:

100000001

 

scribble2.cfm

Same as previous cfml file. Doesn’t created component as I am going to run scribble.cfm page first so object will be in application variables and arguments passed in function called lesser than first one so I get output earlier than first file.

[code:cf]<cfset m = application.mycfc.testMe(1000000)>
<cfoutput>#m#</cfoutput>[/code]

Expected Output:

1000001

Actual Output:

15636065

or

33935923

or

may any other number between 1 to 100000000

This is because index variable ‘i’ which return from function is in variables scope and will be share by both function call. Keep in mind that function called from different pages so their variables will not overwrite by other page variables but to call function we have use same object (application.mycfc) so any variables scope value can be overwritten by another function call and this is what actually happen in this case.

When I run first scribble.cfm it started loop through upto 100000000 and later I run scribble2.cfm which take less execution time then first one because lesser number passed in arguments. Now both function call sharing variables.i and overwrite each other and finally prior to one function call return value another function overwrite value of ‘i’ and gives you wrong output.

Now just add one more statement

[code:cf]<cfset var i = 0>[/code]

under argument tag and see result.

Hope this example give idea of how important to define local scope variable in function.