Ask a Jedi: So what happens when you don't var scope?

This post is more than 2 years old.

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.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by zac spitzer posted on 3/19/2008 at 3:59 AM

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

Comment 2 by Willy posted on 3/19/2008 at 8:04 AM

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

Comment 3 by Kevin Sargent posted on 3/19/2008 at 9:24 AM

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...:)

Comment 4 by Tom Chiverton posted on 3/19/2008 at 1:59 PM

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 :-)

Comment 5 by Lola LB posted on 3/19/2008 at 3:28 PM

I think Ray is still right . . . if you don't know of VarScoper, it still is hard to track down those misbehaving vars.

Comment 6 by Adrian J. Moreno posted on 3/19/2008 at 7:12 PM

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.

Comment 7 by Tom Chiverton posted on 3/19/2008 at 7:38 PM

@Adrian
I hadn't seen the updates it has had.
cfscript parsing is still labeled 'experimental' but it *is* improved, cool.

Comment 8 by Jeremy Prevost posted on 3/19/2008 at 7:55 PM

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).

Comment 9 by Tom Chiverton posted on 3/19/2008 at 8:00 PM

@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."