Work Around ColdFusion Local Scope

These days am working as a tester. I never thought that developer can also do that if they really take interest in testing! Infect someone can also learn from the testing, even testing can also test their understanding for knowledge.

While testing I saw one case where I get more variable in the array object other then I defined. Then I again take a look and I came to know that local variable was causing an issue.

Let me give you some examples and exercise. Make sure that before you see the output just think what would be the output. Consider that all following code is written in sample cfml page and we are using ColdFusion 9.

Case 1:
[code:cf]
<cfset var local= "a">
<cfdump var="#local#">
[/code]

Case 2:
[code:cf]
<cfset local= "a">
<cfdump var="#local#">
[/code]

Case: 3
[code:cf]
<cfset local = structNew()>
<cfset local.x= "a">
<cfdump var="#local#">
[/code]

Case: 4
[code:cf]
<cfset x= "a">
<cfdump var="#local#">
[/code]

Case: 5
[code:cf]
<cfset local.x= "a">
<cfdump var="#local#">
[/code]

What should be the output?

Case 1: We are trying to use var keyword outside or without function. This is not allowed and will give compile time error.

Case 2: We get "a" as output. But will it have any effect?

Case 3: We’ll also get "a" but in structure

Case 4: Now x is in page level variable, so do we get any error that local is not defined as variable? Or empty string? Well we’ll get page argument as structure. In contrast with var, whole page is considered as a function.

Case 5: a will be appended to structure. Now again take a look to case 3. We override the page’s local structure that is why we don’t get arguments scope in local. But it is true? We’ll see later…

Now we’ll proceed with function, where we actually use it.

Again following examples are simple in cfml page.

Case 6:
[code:cf]
<cffunction name="tester" returntype="any" access="public">
<cfargument name="dummy" default="asdfs">
<cfscript>
local = structNew();
local.myname = "Manoj";
local.myjob = "Developer";
</cfscript>
<cfreturn local>
</cffunction>
<cfdump var="#tester()#">
[/code]

What will be the output?
Here we’ll also get argument variables! Again compare with the case 3. These means we cannot override local structure in function or something else?

Now going to little different case, let’s create local variable with local as a name

Case 7:
[code:cf]
<cffunction name="tester" returntype="any" access="public">
<cfargument name="dummy" default="asdfs">
<cfscript>
var local = "x";
</cfscript>
<cfreturn local>
</cffunction>
<cfdump var="#tester()#">
[/code]

What is the output, friends?? Ha ha, now you come to know that, local is an explicit scope, and in above example you are trying to assign string value to local scope. Removing the var keyword it’ll allow me to create a variable named local.

Case 8:
[code:cf]
<cffunction name="tester" returntype="any" access="public">
<cfargument name="dummy" default="asdfs">
<cfscript>
local = "x";
</cfscript>
<cfreturn local>
</cffunction>
<cfdump var="#tester()#">
[/code]

Wait and think again about my same question! You are returning a scope here dude, It’ll also give me arguments variable. So returning a local.local will give the value x? It’s magic or something else? Still local variable is not created!! You will get error if you use local.local.

Case 9:
[code:cf]
<cffunction name="tester" returntype="any" access="public">
<cfargument name="dummy" default="asdfs">
<cfscript>
local.local = "x";
</cfscript>
<cfreturn local.local>
</cffunction>
<cfdump var="#tester()#">
[/code]

Above is the correct way to create local variable in local scope. Hey but still where is the local variable with value x in case 8? Keep on thinking…..
It is in variable scope of the page. I also forgot this thing when I write this post!!

Picture abhi baki hain mere dost! (Still movie is pending my friend)

Case 10:
[code:cf]
<cffunction name="tester" returntype="any" access="public">
<cfargument name="dummy" default="asdfs">
<cfscript>
local.a = "x";
var b = "x";
</cfscript>
<cfreturn local>
</cffunction>
<cfdump var="#tester()#">
[/code]

What is the output???
Yes we’ll get both a and b in local. Using var or local scope does the same thing.
Then what is the difference between var and local?
I already stated that local is an explicit scope, especially used of function.

Remember that I stated in explanation of case 5, that we override the local structure. So this means that if we use function, local becomes scope. While at page level, it is just a simple structure with arguments scope. I hope I am correct in this post. If you have any thoughts or questions, then please feel free to comment.

I took some reference from ben forta’s blog post.
I found another interesting stuff regarding local in Ben nadel’s post.

I hope some of you enjoyed with learning…

Thanks,
Vikas Patel.