Cold fusion best practice


Published on

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Cold fusion best practice

  1. 1. 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
  2. 2. 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.
  3. 3. 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)
  4. 4. 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.
  5. 5. 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
  6. 6. 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.
  7. 7. Use cfqueryparam  Always use cfqueryparam.  You are adding big security loop hole if you are avoiding it.  It also boost up performance.
  8. 8. 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.
  9. 9.  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
  10. 10. 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.
  11. 11. 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.