Best Practices in ColdFusion Development
This article is a compendium of best practices for working with ColdFusion, divided into the following useful categories: files, speed and performance, database issues, security issues, and general issues. That means that these are guidelines for you to follow as you develop. While your code can work if you don't do these, it might not perform as quickly, and it might be harder to follow. I urge you to come up with your own best practices as you work and pass them on to others for consideration.
Writing ColdFusion templates is relatively easy. This is terrific because it means that you can get complex applications up quicklyprobably a fraction of the time it would take you in JSP, for instance. That can make it tempting to rip through your code baseperhaps some developers don't take enough time in the writing phase. So here are a number of easy tips that can keep your code smart, robust, readable, and reliable.
Comment Your Code
At the top of every template, include the following information as comments:
Date last modified
Use (what pages call the template, the chain of events that the user might set in motion to call this particular page)
Purpose (what does this template do?)
Here is an example:
<!----- Template: act_AdAffiliateInsert.cfm Author: Eben Hewitt, eben@CoreColdFusion.com Purpose: Inserts a new Ad Affiliate profile into the Users table. Use: called from Step 2 of act_AdAffiliateSignUp.cfm Date Created: 11/22/01 Date Last Modified: Thursday, November 22, 2001 3:28:14 PM ----->
You know exactly what will be impacted if you change something, and you can easily debug this and its constellation templates. I urge you to follow this practice.
Always Use Application.cfm and OnRequestEnd.cfm
Even if you do not have a use for these files, ColdFusion will go hunting for them on every request. Your page-processing time will speed up just by including empty files with these names in your site folder.
Let us now be disabused of a commonly held yet fallacious belief: The truth is that ColdFusion will search all the way up to the root of your server's hard drive for an application.cfm file. That is, it stops only when it gets to C:\. Don't make it do all of this work, and don't let your applications be potentially exposed to a wandering application.cfm somewhere up the tree.
Set Defaults Only Once
This rule is stated as a corollary of the above because it involves application.cfm. Many developers like to set application-wide variables, such as a title or a datasource in application.cfm. But they don't check to see if their variables are defined, so they end up running the same code over and over again. Use a check to see if a variable has been set. If it has, then move on. This will be slightly faster, and it is better programming form.
<cfif NOT isDefined("application.Datasource")> <cfset application.Datasource = "MyDatabase"> </cfif>