Check out varScoper

This post is more than 2 years old.

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.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by CoolJJ posted on 4/21/2008 at 6:43 PM

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.

Comment 2 by DanaK posted on 4/21/2008 at 6:49 PM

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

Comment 3 by Dennis Spaag posted on 4/21/2008 at 7:50 PM

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.)

Comment 4 by Raymond Camden posted on 4/21/2008 at 7:53 PM

Dennis - I can't take credit for the design - it was a freebie. My design skills are the suck.

Comment 5 by shag posted on 4/21/2008 at 9:02 PM

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...

:-)

Comment 6 by Raymond Camden posted on 4/21/2008 at 9:12 PM

Heh, with close to 3k entries, I'm surprised I don't have more dupes. ;)

Comment 7 by Rob Gonda posted on 4/22/2008 at 1:08 AM

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...

Comment 8 by Will Tomlinson posted on 4/22/2008 at 7:23 AM

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!

Comment 9 by Raymond Camden posted on 4/22/2008 at 5:32 PM

You only var scope the first line. That's the only variable you made. Everything else you did was INSIDE that variable.

Comment 10 by Ben Davies posted on 4/24/2008 at 6:13 AM

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

Comment 11 by Mike Schierberl posted on 4/29/2008 at 4:11 AM

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)