Ask a Jedi: Listing all var scoped variables
Dale asks about var scoped variables:
I think the answer is no, but it seems strange. All variables go into a scope somehow.But if I declare 5 variables as var within a function, coldfusion knows that they are only for that function, but how can I get to them? Is it possible to dump all var variables for a given function? Where are they hiding, are they a struct withing some other scope.
If I dump all the scopes, variables, this, sesssion etc would they simply not exist.
I normally use <cfset var local = structNew() />
Which gets around this, but really wondering where the variables live.
You are correct. Var scoped variables are "special" and currently there is no way to enumerate them in ColdFusion. That is exactly why some people use a local struct as you have described above. Yes, ColdFusion does know about them, and yes, it is a bit odd that they don't have a proper scope, unlike every single other variable out there.
Personally I don't like using a struct for my var scoped variables. It just seems like too much to type. Of course the flip side is that I'm typing more var scopes. I also kind of like seeing the var scope lines as it helps remind me what is going on inside my function.
Comments
Further, I don't see how you have "less chance" of a variable not being in VAR scope. Forget to VAR a cfquery name and your query will still hit the cfc's scope, added struct or not.
From a cumulative performance standpoint, this is a bad thing.
The private var scope idea I read about in the comments of this entry (see Matthew Lesko's and Todd Sharp's comments):
http://www.firemoss.com/blog/index.cfm?mode=entry&...
As for less chance if you have a big function, say, 50 lines plus, and you're half way through it and working on a loop, it is much easier to add cfloop index="private.i" than it is to do index="i" and go to the top of the function and do cfset var i = 0. Thats all.
And I take it back about saving ten seconds, as you now need to type "private." in front of every variable reference. Not to mention the fact that if you now FORGET to type "private." in front of some variable name you've now blown the VAR scope again, but for a different reason.
Just a bad coding practice in my book.
I don't use the private or local convention myself, but I've tempted to consider adopting it, precisely because it allows for all of your variables "looking" scoped. If you have any variable that has less than 2 words, you know it wasn't scoped correctly.
And I think you've posted on another blog about some other programming concern where someone was using structs, and you warned about performance.
First: most people are not scaling to the point where performance (outside of really poorly designed algorithms) really matters. If the struct helps to design cleaner code, eliminating it for performance is premature optimization.
Second: adding a struct really doesn't add that much in way of processing. I ran a very basic test (I know -- looping is a bad testing mechanism). But creating a million structs seems to take around 11 seconds or less on average. So, that's around 0.01 milliseconds per struct. Only Google, Amazon, or Ebay really needs to worry about shaving off that kind of time.
---> "VAR a cfquery name" : Does anyone have an example of how to do this? I haven't seen it before. Thanks.


<cfset var private = {}> (replace {} with StructNew() for pre-8)
and then use the private structure for all my var variables. To me this is a) consistent with how CF works and b) leads to less chance of having variables in cfcs that are not in the var scope and c) means I can create a new var variable half way through a function.
You can dump the var scope with this:
<cfdump var="#getPageContext().getVariableScope()#">