Naming Variables
When it comes time to create your variables, there are some naming rules you must follow:
Variable names must start with a letter, an underscore character, or a Unicode currency symbol. Examples of valid variable names are FirstName, _department, or $userID.
After the starting character, variable names can contain any number of letters, underscores, numbers, or Unicode currency symbols.
Variable names must not contain spaces.
Simple variable names must not contain the dot (.) character. For example, setting a variable name to Sales.Manager will actually create a structure. (For more information on structures, see Step 10.)
NOTE
A database query result is treated as a variable and must not have the same name as any local variables in the same ColdFusion template. See Step 3, "Databases and SQL," for more information on queries.
Provided you follow the preceding rules, you can call your variables just about anything you like. However, most developers follow some sort of naming convention for their variables. Naming conventions make your code more readable and easier to work with, for both yourself and other team members.
For example, ColdFusion variables are not case sensitive. This means that as far as ColdFusion is concerned, firstname, firstName, and FirstName all refer to the same variable. However, most developers use a standard convention with regard to case usage. Java developers, for instance, usually start variable names with a lowercase character and use an uppercase character to mark the start of each new word in the variable name. The variable name employeeStartDate would be an example of this convention. Many ColdFusion developers use a similar convention but also start variable names with an uppercase character, as in EmployeeStartDate. We will be following the latter convention throughout this book.
Here are some additional guidelines for working with variable names:
Avoid using abbreviations. The variable name EmployeeStartDate will make sense when you revisit your code in six months time; the variable name ESD probably will not.
Use a consistent naming convention and capitalization scheme for variable names.
When using forms to update database fields, make the form field names match the database field names. (For more information, see Step 6, "Updating, Inserting, and Deleting Database Records.")
Always use the variable's scope prefix for any scope other than the Variables/Local scope.
Many ColdFusion developers also like to use variable name prefixes to describe the type of data that a variable contains. For more information on data type prefixes, see the following sidebar.
"What's in a Name?"
Many developers like to use prefixes with their variable names to describe the type of information that variable is meant to contain. This way, when any developer comes across a variable somewhere in code, they can instantly tell what type of data it is meant to contain.
Table 2.3 lists some common prefixes.
Table 2.3 Data Type Prefixes
Data Type |
Prefix |
Example |
Array |
a |
aAnArrayVariable |
Boolean |
b |
bChecked, bFlag |
Date |
d |
dEmployeeStartDate |
Date-time |
dt |
dtLastVisit |
Integer |
i |
iAge, iDaysTillXmas |
List |
l |
lFavoriteColors |
Number |
n |
nValueOfPi |
Query |
q, qry |
qProducts, qryDepartments |
String |
s |
sFirstName, sLastName |
Time |
t |
tStartTime, tFinishTime |
Structure |
st |
stMyStructure |