Reserved and Why

 
Jan 17, 2000
by Michael Dinowitz

In Knowledge base Article 14192 (listed above), Allaire has put out a new list of words that are reserved in ColdFusion. No disrespect to them, but they really didn't go into why these words are reserved or where. I'll be going over the three basic groups of reserved words, where they are used and why they are reserved.

Before we start, let me define the term 'word' as meaning any string of letters, numbers, or the underscore, that starts with a letter and can be used as a variable. This definition is actually very important and we'll be using it frequently.

Expressions

As many of you have heard from me in the past, there are places in ColdFusion (called Evaluation Zones) where any word will be automatically treated as a variable and evaluated. Two of these places are the CFSET and CFIF tags. Because of this, ColdFusion reserves certain words to be used as operators and refuses to evaluate them. These words include operators for Arithmetic, Boolean, and Comparison statements.

AND CONTAINS EQ EQUAL
EQV GT GTE IMP
IS LT LTE MOD
NEQ NOT OR XOR

Interestingly, the words DOES, CONTAIN, GREATER, LESS, THAN, and TO are not reserved by ColdFusion. (I prefer not to use these words as variables just to be on the safe side.) I believe that the reason has to do with how ColdFusion evaluates the entire CFIF statement. Notice that all the reserved words can be used singly, while the words that are not reserved are part of a longer statement like 'GREATER THAN.' I believe that if ColdFusion can not find a single comparison operator, it looks at the word combinations in the statement to see if any of them combine to make one of the longer statements. Theoretically, this should add some time to the processing of a CFIF that uses the longer comparison operators, but this is not the case. Any time differences would be very miniscule (and they are).

A final note about this group of reserved words is that they can NEVER be used as the variable in a CFSET. ColdFusion automatically recognizes them and prevents their use.

Scopes

The second group of reserved words are rather simple to find and understand. They comprise all of the variable scopes used in ColdFusion. The reason for this is that ColdFusion places almost all of its stored data into structures. In ColdFusion 3.0, session variables (to name just one) were structures. In ColdFusion 4.5, even Url, Form and CGI variables are in structures.

VARIABLES URL FORM CGI
CLIENT SERVER SESSION APPLICATION
ATTRIBUTES REQUEST THISTAG CALLER
COOKIE CFFTP (CFFTP) CFHTTP (CFHTTP) ERROR (CFERROR)
FILE (CFFILE) CFCATCH (CFCATCH)

This does not mean that you can not use the name of a scope as a variable. Unlike the previous group of reserved words, you can use one of the scopes above as the name of a variable. CFSET will not stop you or throw any sort of error. On the other hand, it will overwrite the structure that already exists. This can have the effect of destroying a user's session by setting a variable named session to some value. Not a great idea.

Also note that while I included ALL of the scopes in the table above, some of them are not actually structures. Variables in the Server and Variables scopes are all stored by ColdFusion as individual units of data rather than as structures. I expect that to change for the Variables scope eventually, but Allaire has held off turning the Server scope into a structure for security considerations. (You can hide stuff in the Server scope and there's no way anyone can access the variables without knowing their exact names.)

CFSCRIPT

The final group is rather special, as they are only reserved in a special location; the body of a CFSCRIPT tag. As with the CFSET and CFIF tags mentioned above, the body of the CFSCRIPT tag is an evaluation zone. This means that as with the comparison and other operators, certain words are reserved by ColdFusion to act as commands. These words comprise ALL of the special code commands used in CFSCRIPT. This is actually all documented in the CFSCRIPT section of the ColdFusion documentation.

FOR WHILE DO IF
ELSE SWITCH CASE BREAK
DEFAULT IN CONTINUE

In the past, ColdFusion did not have a lot of reserved words nor any hard rules for what variable names to use. As ColdFusion becomes more of an industry-accepted 'language,' it is getting more structured, and the rules of when and where you can do something are getting more specific. This is actually a good thing, as it will help prevent people from writing sloppy code. I can just see someone writing a value to a variable called Url and then not having access to his passed Url variables anymore.

Privacy | FAQ | Site Map | About | Guidelines | Contact | Advertising | What is ColdFusion?
House of Fusion | ColdFusion Jobs | Blog of Fusion | AHP Hosting