Var Scoping Tool
So, pre-MX my big thing was locking. Post MX (really, CF5) my big "let me get all anal" thing is var scoping. As you have heard me say time after time on this blog, if you forget to var scope, you will end up in big trouble. (That's right, big trouble. Human sacrifice, dogs and cats living together - mass hysteria.)
Mike Schierberl has released a cool new tool to help solve that problem, the varScoper. This tool lets you examine your source code for potentially missing var statements. It will return a report of all the possible matches.
Whats cool about this tool is that it even found mistakes in my code. Not to say I'm perfect - but I know I definitely look out for it and I still missed things. There is an online demo which uses a copy of BlogCFC as one of it's samples. It found a few false positives, but it found some real issues as well. (Fixed in the latest release!)
Comments
I'm off to run our code through it :-)
I think so!!!
Cool!!
in regards to this var scope I do things a little different..
What about doing something like this?
I var the struct then return that in cfreturn
<cffunction name="foo">
<cfscript>
var stReturn = StructNew();
stReturn.bSuccess = True;
stReturn.message = "";
stReturn.data = "";
stReturn.stError = structNew();
</cfscript>
<!--- Do some query here qGetSomethingFromDatabase --->
<cfscript>
if( Not qGetSomethingFromDatabase.RecordCount )
{
stReturn.bSuccess = false;
stReturn.message = "Some message about no records found"
}
else
{
stReturn.bSuccess = true;
stReturn.data = qGetSomethingFromDatabase;
}
</cfscript>
<cfreturn stReturn>
</cffunction>
The code example above is 90% ok. Scoping a struct with a var statement works, in this case though my tool wouldn't pick it up because it is in cfscript. Also, in your case you would definitely want to include the following statement as well.
var qGetSomethingFromDatabase = "";
cfquery statements create variables based on the name attribute, and as a result could be thread unsafe.
-Mike
Could you go into detail as to why you don't care for creating a local struct as a shortcut to make sure all the variables in your functions have a "local scope".... as in the following example, posted in various places across the internet:
<cffunction name="myFunction">
<!--- define a local scope --->
<cfset var local = structnew()>
<!--- use whatever vars we like in the local scope --->
<cfset local.firstvar = "Hello">
<cfset local.another = arraynew(1)>
<!--- run a query without declaring the var first --->
<cfquery name="local.qMyQuery" datasource="blah">
...
</cfquery>
</cffunction>
http://www.briankotek.com/blog/index.cfm/2006/10/5...
http://www.bennadel.com/blog/94-LOCAL-Variables-Sc...

