0

VAR scoping using structures

ColdFusion
At this stage in the game of ColdFusion, it is common knowledge why you must var-scope all variables in your methods in order to protect your data.  In case this is something new to you, say you set a variable named "myVar" in method A.  Say you had a method B and in it you had another "myVar" with a different purpose.   Unless you declare your variables like this: <cfset var myVar = "something" />  that data is shared between both methods and can be overwritten.

So typically when you have a method you var-scope all your private variables including queries or anything else like this:

<cffunction name="a" output="false" returntype="void">
    <cfset var myVar = "" />
    <cfset var qQuery = "" />

    <..... then all your code ...>
</cffunction>


One thing I have never been fond of is that once this is done there is effectively no visible difference in your code between variables that are var scoped and variables that aren't.  For example, let's say I var-scope mVarA and don't var-scope myVarB.  When I access them in the method they visibly appear to be in the same scope... like this:

<cffunction name="a" output="false" returntype="string">
    <cfset var myVarA = "" />
    <cfset myVarB = "" />

    <cfreturn myVarA & myVarB />

</cffunction>

Looking at this, there is no visible way to see which of those variables is var-scoped without looking at the top of your method to see what you have done.   Plus, as you add variables to your methods, that var-scoping area will grow and you can have a large section of protected declarations.  I have found a different way to approach this by var-scoping a structure named "private" at the top of my methods.  Then in my code I can simply use <cfset private.myVar = "" />.  For example, the code above would become:

<cffunction name="a" output="false" returntype="string">
    <cfset var private = structnew() />
   
    <cfset private.myVarA = "" />
    <cfset myVarB = "" />

    <cfreturn
private.myVarA & myVarB />
</cffunction>

By doing this, I no longer have to go var-scope each individual variable, plus the code becomes far more obvious as to which variables exist in that "private" scope. 
tags:
ColdFusion
Matt Williams said:
 
I like this idea. Do you think using var. for the structure would work? Such as,
cfset var var.qQuery = ""
 
posted 975 days ago
Add Comment Reply to: this comment OR this thread
 
 
Matt, I just tested it and it seems to work. I kind of expected this line to barf....


cfset var var = structnew()


... but it didn't!

I may have just changed my standard. :)
 
posted 975 days ago
Add Comment Reply to: this comment OR this thread
 
 
Matt, one other thing... I noticed in your comment you are re-var'ing it in your cfset. By var'ing the structure, you don't have to declare any other 'var's. You would just say:

cfquery name="var.myQuery"

...and it would already be in the var scope as an element of the var structure.
 
posted 975 days ago
Add Comment Reply to: this comment OR this thread
 
Matt Williams said:
 
Gotcha. I meant what you said. I just tried the same and it worked for me to. In Eclipse, the var keyword is color coded even if it is part of the struct. So I'll have to decide if I like that or not. On one hand, it stands out and I really know what kind of variable it is. On the other hand, it could get confusing. Not to mention the intial 'cfset var var' looks like a typo.
 
posted 975 days ago
Add Comment Reply to: this comment OR this thread
 
 
LOL those were my exact thoughts!
 
posted 975 days ago
Add Comment Reply to: this comment OR this thread
 
Ryan Everhart said:
 
Sounds good Dave thanks for sharing!

Ryan
 
posted 975 days ago
Add Comment Reply to: this comment OR this thread
 
Matt Williams said:
 
While stealing your array iterator code, I thought I'd post an update to this blog entry. I settled on
var vars=StructNew();
vars.myVar = "Foo";

Using var for the name of the structure was confusing me a bit. I've seen some other code using 'local' for the struct name.

Keep up the good work...
 
posted 917 days ago
Add Comment Reply to: this comment OR this thread
 
 
I like that approach. I have stuck with 'private' for no other reason than the fact that it is clear to me. But 'vars' is certainly equally clear. Regardless, I am still a fan of this approach in general and kind of surprised that it isn't more widely used.
 
posted 917 days ago
Add Comment Reply to: this comment OR this thread
 
Joshua Scott said:
 
Actually Dave,

I think the proper "scope" name would be local, Because in Java and really in CF private refers to variables that are accessible to the entire component, object, or class.

Thoughts?

- JS
 
posted 898 days ago
Add Comment Reply to: this comment OR this thread
 

Search

Dave Shuck for President!