by Michael Dinowitz
Evaluation can be divided up into two zones. The first, called an evaluation zone, is a location where variables and functions can be evaluated1 without the need for pound signs (#)2. In addition, it is the only location where expressions3 will be evaluated. The other location will be termed an output zone. This is a place where a variable or function needs pound signs (#) to be evaluated. This difference is mostly a cosmetic one and has almost nothing to do with code tightness and speed. On the other hand, proper usage of pound signs (#) is a sign of a good ColdFusion programmer4.
| Location | Needs # | Variables Functions | Text needs quotes | Processes Strings | Expressions |
|---|---|---|---|---|---|
| Evaluation Zones | |||||
| Right hand side of a CFSET statment | No | Yes | Yes | Yes | Yes |
| Anywhere inside a CFIF statment | No | Yes | Yes | Yes | Yes |
| Inside any Function | No | Yes | Yes | Yes | Yes |
| Inside Array/Structure brackets ([]) | No | Yes | Yes | Yes | Yes |
| Inside a CFSCRIPT tag block5 | No | Yes | Yes | No | Yes |
| Output Zones | |||||
| Inside the body6 of a ColdFusion tag (not CFSET/CFIF) | Yes | Yes | Yes | Yes | No |
| Inside a CFOUTPUT tag block | Yes | Yes | No | No | No |
| Inside a CFQUERY tag block | Yes | Yes | No | No | No |
| Dynamic (Left hand side) CFSET | Yes | Yes | Yes | Yes | Yes |
The way to embed a function or variable within a string is rather easy. You simply wrap the variable or function inside pound signs (#) and it'll be seen by ColdFusion as something to be evaluated. Stylewise, this is something that should never be done within an evaluation zone, only within output zones. In past versions of ColdFusion, there was a speed penalty for this but that penalty seems to be gone in version 4.5.1.
Inside evaluation zones, the proper way of using variables with strings is to treat them like expressions and concatenate them together. This will tell the ColdFusion engine to take the string and append the value of the variable or function to it. As above, this is seen as 'tight and proper' coding and makes people think you know what you're doing. :) On the other hand, there is a time and place where it is better to write your strings and variables/functions together. That time is when you are in a rush and have no time and that place is where you will have a LOT of text and variables mixed together.
CFSET is a special case even among evalaution zones, as it has some added functions. The standard function of the tag is to take some value (assigned on the right hand side of an equal (=) sign) and assign it to a variable name that is on the left hand side of the equal sign (=). This is rather straightforward, until you want to dynamically create the name of a variable. This event breaks the rules of using variables. To create a dynamic variable, you place the text AND the variable inside of quotes. This is basically what we described above. It'll work and it is useful, but using the SetVariable() function is stylistically better.
<CFSET var1="name"> <CFSET "first#var1#"="Michael"> |
This results in a variable called firstname with a value of Michael.
<CFSET Form.list="1,2,3,4,5"> <CFSET list2="6,7,8,9,10"> <ListGetAt(form.list&list2,> <randrange(listfirst(form.list),listlast(list2)))> |
The order of operation here is:
Now you see why we don't want to think about this. It's tedious and obvious once you look at it. Evaluation moves from left to right and from inside out. As a side note, this is also the order of operation for any complex statement using functions and/or expressions such as the code inside the brackets([]) of an array or structure call.
Evaluation is one of the cores of ColdFusion and having a strong understanding of it is a must. With a deep understanding of it, you can create some really complex code. In my next article, I will discuss how to use advanced evaluation with the Evaluate() function to create custom functions in ColdFusion. While these are not true functions, they are a useful and interesting hack.