2. SCOPE YOUR VARIABLE
Always specify Scope of variables, this tells
coldfusion where to look for variable value and
gain performance.
What’s different between #URL.firstname# and
#firstname#?
Scope Evaluation Sequence
Arguments
Variables (local scope)
CGI
URL
Form
Cookie
Client
3. Use Boolean evaluation
Avoid IS, NOT IS where ever possible.
<cfif arguments.firstName IS “”> equal to
<cfif len(arguments.firstName) eq 0> equal
to
<cfif len(arguments.firstName)>
All above statement work same only
difference is performance.
And this become remarkable on high traffic
sites.
4. AVOID IsDefine
How do you check form.firstName variable exist or
not?
Really simple <cfif isDefined(“form.firstName”)>
Ever try <cfif structKeyExists(form,”firstName”)>?
Second Method gives you almost double performance
gain.
User structure function instead of isDefined to check
existence.
Session, form, application, URL, arguments all are
structure variable. (Believe me)
5. AVOID Evaluate()
Evaluate is Evil.
Generally we use Evaluate function get
value from dynamic variables name.
From.text1, form.text2, form.text3 can be
obtain with evaluate(“form.text” & k) where k
varies from 1 to 3.
Wait we have another way to do so.
○ Form[“text” & k] (K varies from 1 to 3)
Second method gives around 3-5 times
performance gain.
6. Use of CFOUTPUT
I think CFOUTPUT is most common tag
use by Cfer and most widely use in
application.
Beware unnecessary use of any tag, it
cost.
Performance Gain 2 to 8 times
<cfloop from=“1” to=“10”
index=“I”>
<cfoutput>#i#</cfoutput>
</cfloop>
<cfoutput>
<cfloop from=“1” to=“10”
index=“I”>
#i#
</cfloop>
</cfoutput
7. AVOID over use of #
# often use to output variable values.
<cfoutput>#name#</cfoutput>
Sometimes to use variable in string.
<cfset a = “Hello #name#”>
Other than this I don’t think # sign needed.
Do not use in.
<cfset #name# = “John”>
<cfif #name# eq “John”>
Will not fire any error but we have lots of other
thing to load ColdFusion.
8. Use cfqueryparam
Always use cfqueryparam.
You are adding big security loop hole if
you are avoiding it.
It also boost up performance.
9. Best Practice for CFC
In CFC allow access to data associated with CFC
only.
Do not use invoke or creating component to calling
function from same CFC.
Avoid use of this as it for variable scope.
CFC is component and do not use it for presentation
layer (view).
Always use output = “false” for function and on
component. In case you need it return as string
instead of printing output in CFC.
Do not directly refer to external variables (variables,
session, Application). In some case application
variable is ok but never use session or variables.
10. Specify arguments type in function
arguments to avoid unnecessary error and
it improve security.
Specify return type of function wherever
possible.
Best Practice…
Create structure variable LOCAL using var
declaration.
Specify scope as LOCAL for all function’s
variables.
○ Give more readability.
○ Avoid variable overwrite
11. Keep in Mind
Variables type array, string, number and
dates all pass by value.
Variables type structure, query and other
complex variable pass by reference.
12. Things we already know
Always give well defined name of
variables.
Use camel case for variable name and
avoid underscore.
Always write something meaning full
comments instead of making it horrible.
E.g. “Very complex logic, do not touch
this code”.
Use hint in cffunction and cfargument.