Posted in ColdFusion | Posted on 03-18-2008 | 4,982 views
Jason asks:
I know it is important to ALWAYS var scope your variables in your CFCs my question is, is there a difference between these two cfset statements?
<cfset var myVar = "" />
<cfset variables.myVar = "" />
It is indeed very important, and there is a world of difference between those two lines of code. I cover this more in my CFC Scope Reference, but the basic difference is that your first line of code creates a variable that will only exist for the execution of the method. You use this for any variable you create in the method, and be sure to not forget things like loop counters, query names, etc.
The second line creates a global variable the CFC. Anything in the variables scope is available to every method of the CFC. I'll typically use the variables scope for configuration information, like a DSN, so all my queries will use datasource="#variables.dsn#". You would definitely not want to use it for things you intend to just be in the local method.


Thanks, that answered my question perfectly. And now that it is explained, it makes perfect sense. i can;t believe i didn;t see it before.
Jason
<cfset var myVar = "" />
and
<cfset var.myVar = "" />
Are these two statements the same within a cfc?
cfset var local = structNew()
and then use local.x, local.y, etc. This means less var scoping and makes it a bit more obvious that a variable is a local var, but personally I don't care for it.
Can Should you
<cfset var intx = 0>
<cfloop from="1" to="#q.recordcount#" var=intx>
so that it's in the local scope, or doesn't it matter?
...Then again, maybe they don't do that for some reason that I'm not aware of. So until then, just one of those things I must keep memorized.
Var scoping them just seems kind of... silly.
var scoping may be silly, but it's used in other languages as well.
Also, when you have a cffunction with a lot going on in it, it's sometime preferable to do cfset Var local = StructNew() right below your arguments, and then prepend all your function-"local" variables with "local." to ensure you don't miss anything. Usually, your functions should be small enough that it doesn't get that complicated, but the real word isn't always so simple... ;)
What I meant by silly is that everything should be local unless specifically made global... it just bugs me a little.
Thanks for the tip Ray.
<cfset var "#arguments.passedInVariableName#" = "" />
or
<cfset var setVariable(arguments.passedInVariableName,"") />
cfset var local = structnew()
cfset local[anythinggoes] = "foo"
I've since just statically renamed these variables, but your suggestion would have been just as well, if not more elegant.
If you use local[anythinggoes], (i.e. <cfquery name="local[#anythinggoes#]"...> ) CF throws an error because the name local[theactualvalue] "is not a valid ColdFusion variable name".
I may be wrong, but if you do <cfquery name="#local[anythinggoes]#"...> the query results would not be locally scoped since #local[anythinggoes]# would just be a placeholder for the real variable name.
Like I mentioned before I'm now just using a static name for the query and that is fine (and probably the only way to do it). In this case, the dynamic naming of queries is not for referencing the query object itself (inside or outside) of the function, but is simply so that debugging output displays the descriptive name for the query. For instance:
<cfscript>
myQuery = queryCFC.read(sql="select * from orders", name="getAllOrders");
</cfscript>
The debugging output would show the query name as "getAllOrders". petty I know.
cfquery name="mydata"
local[dynamicnamehere] = mydata
Maybe there's something I'm missing?
[Add Comment] [Subscribe to Comments]