Avoid use of scope names for argument names

This falls under the category of "I knew better..." I'm working with a client who is using my YouTube CFC product. They were encountering some problems and wanted me to work on modifications to their page to handle cases where YouTube was down, or simply not returning proper information for whatever reason. (In our case it ended up being a network issues. ColdFusion couldn't hit any remote site due to a network change.)

While working on the development server I made this simple change to my CFC:

<cfif not isXML(result.fileContent) or isDefined("url.xxx")>

This would let me quickly toggle back and forth between a good connection and a forced YouTube breakdown. But something odd happened.

When I added ?xxx=1 to my URL, the error condition didn't fire. I checked to ensure I wasn't caching, reuploaded, etc, and nothing worked.

Turns out I had used URL for the name of one of the arguments to the function:

<cfargument name="url" type="string" required="true">

I typically avoid doing this. All I can think of in this case I figured since I always use "arguments." when referencing arguments, there wouldn't be any confusion. But that wasn't the case. As soon as you add an argument like this, you essentially "break" the URL scope. (Switching to structKeyExists didn't help.)

I renamed my argument to "theurl" and then everything worked fine.

Archived Comments

Comment 1 by Shane Zehnder posted on 11/16/2007 at 4:21 AM

I've done that more times than I am likely to publicly admit again. Now I make a mental note not to name a variable anything remotely close to any scope.

I see it can even happen to the best of us. :)

Comment 2 by Nolan Erck posted on 11/16/2007 at 4:38 AM

I seem to be in the minority these days, but this is one of the reasons I'm a big fan of some sort of Hungarian/type-prefixing on my variable names. It seriously reduces the possibility that I'll accidentally use a reserved word.

Comment 3 by Dan Sorensen posted on 11/16/2007 at 4:55 AM

Just to clarify your headline, it's still a good idea to use the 'arguments' scope to prefix variables within a cffunction or cfc, just not OTHER scopes right?

Comment 4 by David Herman posted on 11/16/2007 at 5:00 AM

@Dan, Yes but he's saying don't have a variable named form or url or arguments.

Comment 5 by Raymond Camden posted on 11/16/2007 at 5:45 AM

@Dan - David answered, but I just want to be 100% clear. I do not mean to avoid using the scope name. I do it for everything but variables scope stuff (and I do use variables. inside of a CFC). I meant - don't name a variable after a built in scope.

Comment 6 by Dan Vega posted on 11/16/2007 at 6:40 AM

This happens to everyone! I was just working on a project not to long ago where the business object was actually a Client so whenever I called client.xxx is was not defined. As well all know client is already a scope so stay away from reserved words and scopes is the moral of this story! Thanks for the reminder...

Comment 7 by Jared Rypka-Hauer posted on 11/16/2007 at 7:14 AM

OMG! Ray! Yer a n00b!

hehehehehehehehe

Comment 8 by Brian Panulla posted on 11/16/2007 at 6:08 PM

@Nolan: +1 on the Hungarian-prefixy thing, especially on database column names. There's nothing worse for portability than converting a database to a new type of server than suddenly discovering a the RDBMS treats a column name as a reserved word. ("timestamp" comes to mind).

Maybe that's why Ruby on Rails gives me hives.

Comment 9 by James Marshall posted on 11/16/2007 at 6:17 PM

I'm another that finds Hungarian notation does the job. Sure, it's extra typing, and there are people who think it has no place in a dynamically typed language, but I wouldn't be without it.