Posted in ColdFusion | Posted on 02-08-2010 | 3,966 views
Sorry to yell (well, we don't have to assume all exclamation marks are equivalent to yelling) but this email came in to me this morning and it is very troubling for me:
I have a question for you about varScoper and CF9. As I understand it, in CF9 all unscoped variables in a function are placed into a protected local scope, thereby killing the need to use the var x = "" line to scope them.If that understanding is correct, does this make varScoper obsolete in CF9? Our team in upgrading to CF9 in the near future, and trying to identify if running code through varScoper needs to continue to be a part of our code review processes or not.
The answer is an unequivocal no. You must still continue to var scope your variables in UDFs and CFC methods. There is no change in this respect. ColdFusion 9 only makes two related changes:
- The var scope previous was an "unnamed" scope. ColdFusion 9 fixes this by creating a scope called local. Like other ColdFusion scopes you can treat this as a structure.
- Because there is an implicit local scope, you can now skip the var keyword and use "local." instead. So given the line used in the quote above, you could var scope X by rewriting it as local.x="". To me, this is still "var scoping", it just gets there via a different path.
So long story short - yes - you still need to var scope in CF9. The varScoper tool is still an important and necessary part of your testing/deployment strategy.


cfset var local = {};
and then appending to the struct from there. I realize this is nothing earth shattering as people were doing it years before I even figured it out. What killed me was when I did this by accident in several places
cfset local = {};
That's when I started pulling my hairs out, in pure denial that I did anything wrong. Ugh.
local.x = 1
is the same as
var x = 1
For instance if I do:
<cfhttp url="http://foo.com">" target="_blank">http://foo.com">
and reference #cfhttp.filecontent#
That is not threadsafe!
Totally bit me in the ass on a production application.
Instead you need to do:
<cfset var httpresult = '' />
<cfhttp url="http://foo.com" result="httpresult">
and then reference #httpresult.filecontent#
I had var scoped every variable in my app...except for the one I didn't create myself and let CF create for me.
As of CF9, are you now writing your components entirely in CFScript, or are you mixing CFML and CFScript?
If you are using CFScript entirely, what are you using for VarScoping? Mike's varscoper does not work in cfScript based on components.
dave
[Add Comment] [Subscribe to Comments]