Name vs. Variable In ColdFusion
Posted March 12, 2008 at 8:48 AM by Ben Nadel
There are a good number of tags in ColdFusion that return a value into a variable defined by one of the tag attributes. While you might not think about it, the attributes that define the return variable (excluding the Result attribute of things like CFHttp and CFFile) fall into one of two main categories:
Now, as you might know, I try to avoid using intellisense as much as possible. I definitely take pride in remembering as much of the ColdFusion syntax as I can. And, while I have been coding ColdFusion for a long time, I definitely get tripped up from time to time in remembering which tags use Name and which tags use Variable to define the return value. And so, in order to sharpen my own mental-intellisense, I thought I would write these tags down and then see if there was any common theme I could use to train my mental processes. Here are the tags that I could find:
- CFZip (read / read binary)
- CFHTTP (query)
- CFFTP (query)
- CFZip (query)
I thew in the CFWDDX tag just because it stood out as the very different one. I rarely ever use WDDX so I am not even gonna waste the brain power on it. Plus, I think its a really old tag, so I definitely wouldn't think it would stick to any established standard.
But, let's look at the list of Variable vs. Name attributes. The first thing that pops out is that all tags that can return a ColdFusion query object use the Name attribute. So that's rule #1: query equal name attribute. The second thing that pops out at me is that any text-related return value uses the Variable attribute. This is a bit of a stretch since CFFile and CFZip can both return binary reads, however, they can also return text-file reads. CFSaveContent definitely returns text. CFXML returns an XML Document, but think about it like this - XML is really just a text format and yes, an XML Document is not a text value, but it represents a structured text value. So, I think that's rule #2: text-like equals variable attribute. Of course, you could say the same thing about CFFeed and structured text.... well, I'm just trying to find the most common ground here, plus I heard you drink you own pee, which is kind of gross.
Having written these down and grouped them, it looks like this might not be too bad. I am gonna say that these are the basic rules (with one or two exceptions):
- Text based data / data reads are stored in Variable-defined values.
- Query and other complex data values are stored in Name-defined values.
Yes, that doesn't quite work across the board, but I feel much more comfortable know this.
on a different note in the same tune, the one that always gets me is the use of 'item' in cfloop when you're looping over a collection. I've become conditioned to writing 'index' in my cfloop tags. although I don't have the problem when looping over a query or doing a conditional loop. I guess the fact both attributes start with 'i' and are vaguely similar...
Just wanted to comment something on the cfwddx tag; the cfwddx tag uses the WDDX standard that was created in 1998 by Simeon Simeonov from Allaire. WDDX was first used in Cold Fusion (back when it was two words) and was released publicly a year later. It's a really useful tag for saving/reading configuration files because it defines structures, arrays, strings, and numbers that convert directly to CF elements.
Yup, I'd agree with the comment on WDDX's usefulness. It's great if you need to serialize/deserialize a complex variable in order to store it somewhere for later retrieval. For instance, we use it to store someone's workflow state in our database so that they can log out, then come back later and restore their session. I also use it to save a user preference struct to the server's hard drive for some applications that don't depend on a database.
I definitely make that mistake a lot. I just remember that collection looping is the exception.
The CFWDDX tag is definitely a good tag. I have used it a lot of the course of time (not lately). I have created XML-databases that rely on it heavily. It is pretty awesome that it goes from XML directly to ColdFusion structures. It is good stuff.
Don't forget cfinvoke / returnVariable.
Good call. I'm so used to using CreateObject() that I fogot CFInvoke had a return variable. That's way different than the rest.