So I got a good follow up question today on var scoping (Did someone decide today was Var Scope day? What do I give?) based on my earlier post. Matt asks:
I know that I am supposed to var my variables inside my cfc functions. What happens if I don't var them.
You've probably heard me preach on var scoping many times before, as well as other speakers in the ColdFusion community. So why do we keep ranting on it? The simple answer is that un-var scoped values leak. What do I mean? Consider this simple, non-cfc example. (Don't forget the 'var scoping' rule applies to both CFC methods and UDFs.)
<cfscript>
function doit(x) {
y = 2;
return x*y;
}
</cfscript>
<cfoutput>#doit(10)#</cfoutput>
This UDF simply returns the double of a value. What makes this interesting is if you add a bit of debugging:
<cfscript>
function doit(x) {
y = 2;
return x*y;
}
</cfscript>
<cfdump var="#variables#">
<cfoutput>#doit(10)#</cfoutput>
<cfdump var="#variables#">
When you first run it, the Variables scope only contains the UDF. But when you run it again, y is now defined as 2. So why is this a big deal? Consider this example:
<cfscript>
function doit(x) {
y = 2;
return x*y;
}
</cfscript>
<cfset y = 199>
<cfset doubley = doit(y)>
<cfoutput>
Our original value is #y#, and double it is #doubley#
</cfoutput>
I've taken a variable, Y, and stored a value in it. I then call my UDF to get the double of it - then I output the results. If you run this, you will see:
Our original value is 2, and double it is 398
This is certainly not what we want and it clearly shows the negative side effects of missing var statements. In my example it was relatively easy to track down since the UDF was on page, but in a CFC it could be much harder to figure out. Missing var scopes are one of the most difficult bugs to track down.
Archived Comments
Ray, i would have to disagree with your last statement "Missing var scopes are one of the most difficult bugs to track down." because http://varscoper.riaforge.org/ really does help solve most of the problem.
It should be used as part of every release process
Slightly OT:
We usually do...
<cfscript>
var Local = structNew();
</cfscript>
Then we can do
Local.y = 2;
etc.
Without having to do another var declaration. This cuts down on accidentally leaving it out.
/willy
I also do var LOCAL = structNew()
then you get in the habit of calling EVERY var insode a function / method either LOCAL or ARGUMENTS
very slim chance of un varing a var...:)
VarScoper is a good tool, but it's not perfect (can't do cfscript yet, some times gets confused).
It's also coming to a CFEclipse near you soon too :-)
I think Ray is still right . . . if you don't know of VarScoper, it still is hard to track down those misbehaving vars.
I've got a couple of posts about this. They start here:
http://www.iknowkungfoo.com...
In that post, I've linked to another great example from Dave Shuck that exposes threading issues when you don't use the var scope.
@Tom: The latest version of varScoper has support for cfscript.
@Adrian
I hadn't seen the updates it has had.
cfscript parsing is still labeled 'experimental' but it *is* improved, cool.
VarScoper is quite handy (just ran some of my CFCs through it and it caught a couple of functions I had missed).
@Tom: This in CFEclipse would rock. Any chance of a plugin or script coming that'll de-suck code written before coffee has kicked in? (Maybe in the form of not allowing me to open Eclipse until my caffeine levels reach a specified level).
@Jeremy
http://www.fusionauthority....
"One [thing I'm building for CFEclipse], which I am porting from ColdFusion to Java, is the variable scoper, which finds un-var'd variables in functions."