ColdFusion supports many different variable types, and when referring to variables the type may be specified as a prefix in the format prefix.variable. Many developers believe the best practice is to always use prefixes, but prefixes actually are not required in every instance. The following variables do not need to have a prefix:
NOTE A variable's scope determines where it exists, how long it exists, and where its values are stored. The variable's prefix determines its scope. Reason One to Use Prefixes: PerformanceThe first reason to use a variable prefix is performance. Consider a variable called TheVar that is used in an expression and not prefixed. ColdFusion must find the value for that variable. ColdFusion must look in each variable scope for the TheVar variable and its associated value. The closer the variable is to the bottom of the order of evaluation hierarchy, the longer it will take to retrieve the value of the variable. TIP For variable scopes that do not require prefixes, the order of evaluation is as follows:
Reason Two to Use Prefixes: Avoiding AmbiguityProblems can arise when the same variable name comes from two different scopes. Consider the following two assignment statements: <cfset TestVar="Local"> <cfcookie name="TestVar" value="Cookie"> The variable will be displayed using this statement: <cfoutput>#TestVar#</cfoutput> When evaluated, the value "Local" would be displayed. The COOKIE variable called TestVar is set after the local variable TestVar, but because of the order of evaluation, the local variable's value is displayed when the expression is evaluated. If you want to display the value of the COOKIE variable, you have to use the COOKIE prefix. A Reason Not to Use Prefixes: FlexibilityConsider a template that is both the action page of a form and the target of a URL. A variable of the same name is passed in from each page. In this scenario, by not using a prefix, the template would work in both cases. The nonprefixed variable could act as both a FORM and URL variable. |