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.
Archived Comments
What a sweet project! I thought I was a good var scoper myself. But, looks like I got a little extra work to do today.
yeah varScoper has saved me time and headaches over and over. great tool. This should be one of the staples of anyone using CFCs and UDFs
Does this mantra increase one's chances for enlightenment? You know, I have not been to your site in a while - it looks nice and clean. (I usually just read the feed.)
Dennis - I can't take credit for the design - it was a freebie. My design skills are the suck.
Yeah, I found out about it from the first comment here:
http://www.coldfusionjedi.c...
In searching for the comment, I thought it a bit humorous that you posted about this nearly 2 years ago here:
http://www.coldfusionjedi.c...
:-)
Heh, with close to 3k entries, I'm surprised I don't have more dupes. ;)
Cannot emphasize this enough ... varScoper is a must because you will always forget it somewhere and only realize it when the project is live and under load...
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...
I knew about this tool, but never used it. Just did and it's friggin' sweet!
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!
You only var scope the first line. That's the only variable you made. Everything else you did was INSIDE that variable.
What a great tool. Have you or anyone else heard of way to include it with automation tools (like ANT). I now have mxUnit churning away on each build and adding varscoper seems like a natural addition
Ben-
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)