One of the things you've probably heard me say about a thousand times is to be sure to properly var scope your CFC methods and UDFs. It's one of those 'rules' that us bloggers like to repeat so many times it sounds like a mantra. (I'm waiting for the Var Scope YouTube song to launch.) At my current job (working with Sean as a contractor at Broadchoice) I've been lucky to be exposed to some cool tools that I've never worked with much before, like ColdSpring and Transfer. Another tool I've recently played with is varScoper. This tool will scan a directory of files and look for un-var-scoped variables. Imagine my surprise when it discovered more than one missing var statement in my own code. Not that I'm perfect of course, but I was really impressed with how quickly the tool dug up the issues. It creates a nice HTML based report (it has other formats as well) that is easy to work with. (See screen shot here.)
It did report two false positives. One was a variable that was in the arguments scope, but I forgot to scope it. To me that's a good false positive as I try to always use "arguments." in front of each argument. The other one was an issue with cffeed. I've reported it to the author and I'm sure it will get fixed soon.
I really want to recommend this to folks. As I said - no one is perfect, and with a tool like this, there is no reason not to check your code daily to ensure you don't make any mistakes.
Comment 1 written by CoolJJ on 21 April 2008, at 9:43 AM
Comment 2 written by DanaK on 21 April 2008, at 9:49 AM
Comment 3 written by Dennis Spaag on 21 April 2008, at 10:50 AM
Comment 4 written by Raymond Camden on 21 April 2008, at 10:53 AM
Comment 5 written by shag on 21 April 2008, at 12:02 PM
http://www.coldfusionjedi.com/index.cfm/2008/3/18/...
In searching for the comment, I thought it a bit humorous that you posted about this nearly 2 years ago here:
http://www.coldfusionjedi.com/index.cfm/2006/7/20/...
:-)
Comment 6 written by Raymond Camden on 21 April 2008, at 12:12 PM
Comment 7 written by Rob Gonda on 21 April 2008, at 4:08 PM
Most people don't know what they must var scope their queries, indexes for loops, cfhttp/cfftp return or connection variables, and well, pretty much anything...
Comment 8 written by Will Tomlinson on 21 April 2008, at 10:23 PM
I have a question though. Here's something it caught:
<cfset retStruct = structNew() />
<cfset retStruct.success = false />
<cfset retStruct.errMsg = '' />
<cfset retStruct.imagename = arguments['newImageName']>
Now, do I just need to var scope the first line?
<cfset var retStruct = structNew() />
Or all of them? Since the rest of the variables are a part of the first.
Thanks!
Comment 9 written by Raymond Camden on 22 April 2008, at 8:32 AM
Comment 10 written by Ben Davies on 23 April 2008, at 9:13 PM
Comment 11 written by Mike Schierberl on 28 April 2008, at 7:11 PM
I'm actually planning to post a blog entry on this sometime soon. At my company we have varscoper setup and running as part of our continuous integration process. It's invoked via ANT and reports build failures when a var scope violation is detected. It validates a section of our codebase on an interval when new files have been committed. I'll put something together once I've created a more generic version to release. (sorry to be a tease)
[Add Comment] [Subscribe to Comments]