Evaluation in ColdFusion

 
Aug 21, 2000
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.

LocationNeeds # Variables FunctionsText needs quotes Processes StringsExpressions
Evaluation Zones
Right hand side of a CFSET statmentNoYesYes YesYes
Anywhere inside a CFIF statmentNoYesYes YesYes
Inside any Function NoYesYes YesYes
Inside Array/Structure brackets ([])NoYesYes YesYes
Inside a CFSCRIPT tag block5NoYesYes NoYes
Output Zones
Inside the body6 of a ColdFusion tag (not CFSET/CFIF)YesYesYes YesNo
Inside a CFOUTPUT tag blockYesYesNo NoNo
Inside a CFQUERY tag blockYesYesNo NoNo
Dynamic (Left hand side) CFSETYesYesYes YesYes

Evaluation in Strings

A string is simply text to be viewed or manipulated at will. Normally, a string does not have to have any special evaluation--unless there is a variable or function embedded within it.

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

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.

Order of Operation

Functions have an order of operation when it comes to their evaluation. The reason for this is that you can nest functions and a function can have an expression as an attribute. Before a function can be evaluated, all of its attributes have to be evaluated. If one of those attributes is a function with attributes, then the nested function's attributes have to be evaluated. Let's look at an example:
<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:

  1. Evaluate the expression form.list&list2.
  2. Evaluate the variable form.list.
  3. Use the value of form.list as the attribute for listfirst().
  4. Evaluate the variable list2.
  5. Use the value of list2 as the attribute for listlast().
  6. Use the results of listfirst() and listlast() as the attributes for randrange().
  7. Use the value of form.list&list2 along with the final value of randrange as the attributes for listgetat().

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.


1) Evaluation - 1. The act of turning a variable into its stored value. 2. Getting the final value of a function call. 3. Getting a final value from a ColdFusion expression.
2) Pound Sign (#) - A construct used in ColdFusion code to specify something to be evaluated. Only needed inside of output zones.
3) Expression - A combination of two or more of the following types of data: strings, numbers, variables, functions. These will be combined using operation characters such as the concationation operator (&) or the addition operator (+).
1+1
form.firstname & form.lastname
4) ColdFusion Programmer - ColdFusion has the characteristics of a programming language and anyone writing with it can be considered a ColdFusion programmer. If someone says that it is not a language or you are not a programmer, tell them that Fusion Authority has a special shirt for them (A reference to the FA Mofo shirt given to authors)
5) Tag Block - This is the location between a tag and its closing tag
<CFSCRIPT> tag block </CFSCRIPT>
6) The body of a ColdFusion tag is the location between the name of the tag and the closing bracket.
<CFHTTP tag body >

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