By Michael Dinowitz
CFSET is probably the first tag that anyone learns in ColdFusion. Unfortunately, few learn it efficiently. Eventually, they learn through trial-and-error or from reading docs on how to do it right, but that's usually a while into their career. This paper should change that and serve to pull together all the different documentation about CFSET into one location.
I'm going to lay out some 'laws' of the CFSET tag that I've found to be the most efficient. They'll include when and where to use pound signs (#), how to create dynamic variables and when setting a variable isn't even needed. Please remember that while I call these laws, they're really good suggestions. If you find some practice that works better, use it. If you find something wrong in what I'm writing, please contact me so we can set it right.
There are actually two functions of the CFSET tag. The first is Assignment and the second is Execution . When used for Assignment, the CFSET tag can be said to have two parts. (This is rather important as it will color the terminology I'll be using throughout the article and in almost all my writings.) The first part of a CFSET tag is the Variable. This is the name of a variable that will be set by the CFSET operation. This value is usually static, but can be dynamically created in a special circumstance. The Variable section of a CFSET tag is only used in Assignment; It is not needed in Execution. The second part of the CFSET tag is the Value. This is the information that will be placed into the Variable. This information can be static, dynamic or a combination of the two. We can even use functions as dynamic information. This part of a CFSET is always used. When a CFSET tag is used for Execution, it allows certain ColdFusion functions to be run without trying to assign any value to a variable. In this case, only the Variable portion of the CFSET is used.
A Variable may only contain letters (a-z), numbers (0-9) or an underscore (_). No other character may be used in a Variable. A period (.) is used only to separate a Variable name from its Scope. All Variables are case insensitive.
|
Wrong: <CFSET Name$ = "Michael">
Right: <CFSET FirstName = "Michael"> Right: <CFSET Variables.FirstName = "Michael"> |
A Variable may not start with a number (0-9) or an underscore (_). Only letters (a-z) may be used.
|
Wrong: <CFSET 1stName = "Michael">
Right: <CFSET FirstName = "Michael"> |
Remember to always close off your quotes in a tag.
|
Wrong: <CFSET FirstName = "Michael>
Right: <CFSET FirstName = "Michael"> |
Remember to always close your tags properly.
|
Wrong: <CFSET FirstName = "Michael"
Right:<CFSET FirstName = "Michael"> |
Variables should be as descriptive as possible. ColdFusion does not use more resources to write a long variable vs. a short one.
|
Wrong: <CFSET FNM = "Michael">
Right: <CFSET FirstName = "Michael"> |
All ColdFusion tag attributes should only be in double quotes ("). This is to differentiate them from function attributes which should only be in single quotes (').
|
Wrong: <CFSET FirstName = 'Michael'>
Right: <CFSET FirstName = "Michael"> |
The first character of a Variable should be capitalized. In addition, capitals should be used in various places to make the Variable more descriptive. Finally, a capital should be used after the period in a Scoped Variable .
|
Wrong: <CFSET firstname = "Michael">
Right: <CFSET FirstName = "Michael"> Right: <CFSET Variables.FirstName = "Michael"> |
When setting and/or using a string that contains both text and variables, you should separate the string component from the dynamic variable component and concatenate them together using an ampersand (&).
|
Wrong: <CFSET firstname = "Michael#url.id#">
Right: <CFSET FirstName = "Michael"&Url.ID> |
To expand on the last note, when the Value section of a CFSET only has a dynamic variable or a function, it should not be placed in pound signs (#) and quotes ("). This is commonly done.
|
Wrong: <CFSET firstname = "#Url.ID#">
Right: <CFSET FirstName = Url.ID> |
The Variable part of a CFSET does not need pound signs unless you're setting a dynamic variable name, in which case it should be inside double quotes (") as well.
|
Wrong: <CFSET #Name# = "Michael">
Right: <CFSET "#Name#" = "Michael"> |
When setting a dynamic Variable in a CFSET, remember to place the entire Variable within double quotes (").
|
Wrong: <CFSET #varname# = "value">
Wrong: <CFSET var#name# = "value"> Right: <CFSET "#varname#" = "value"> Right: <CFSET "var#name#" = "value"> |
The Variable part of a CFSET does not need quotes unless it is being dynamically set. This actually has a savings in time of approximately .004 ms.
|
Wrong: <CFSET "Name" = "Michael">
Right: <CFSET Name = "Michael"> |
When a large number (3+) of CFSETS are used in a row, it is more efficient to place them within a CFSCRIPT block. The savings is .02 ms and increases with each CFSET added. When 4 or 5 are used, the savings was .04 ms.
|
Option1:
<CFSET FirstName = 'Michael'> <CFSET Lastname = 'Dinowitz'> <CFSET Email = 'Mdinowitz@houseoffusion.com'> Option2: <CFSCRIPT> FirstName = 'Michael'; Lastname = 'Dinowitz'; Email = 'Mdinowitz@houseoffusion.com'; </CFSCRIPT> |
|
<CFSET Function()>
|
The following functions can be used in this way:
| ArrayAppend | ArrayClear | ArrayDeleteAt |
| ArrayInsertAt | ArrayPrepend | ArrayResize |
| ArraySet | ArraySwap | CF_SetDataSourceUsername |
| CF_SetDataSourcePassword | CFUSION_SETODBCINI | CFUSION_SETTINGS_REFRESH |
| CFusion_DBConnections_Flush | QueryAddRow | QuerySetCell |
| SetLocale | SetProfileString | SetVariable |
| StructClear | StructDelete | StructInsert |
| StructUpdate | WriteOutput |
This is usually used in such statements as the following one, which will delete a user session:
|
<CFSET StructClear(Session)>
|
Now before you get a smart idea about using a CFSET to output variables in place of a CFOUTPUT, let me tell you that it is less efficient.
|
<CFSET WriteOutput(Url.FirstName)>
|
To be continued ...