Home > Articles > Mobile Application Development & Programming > HTML5 & Web Standards

Best Practices in ColdFusion Development

  • Print
  • + Share This
Eben Hewitt offers a collection of best practices to follow when using ColdFusion - neatly organized into topics such as file use, speed and performance, database issues, and security.
This article is adapted from the book Core ColdFusion 5 (Prentice Hall, 2001), by Eben Hewitt.
From the author of

From the author of

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.

Files

Writing ColdFusion templates is relatively easy. This is terrific because it means that you can get complex applications up quickly—probably a fraction of the time it would take you in JSP, for instance. That can make it tempting to rip through your code base—perhaps 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:

  1. Template name

  2. Author's name

  3. Date created

  4. Date last modified

  5. Use (what pages call the template, the chain of events that the user might set in motion to call this particular page)

  6. 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.

NOTE

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.

Like this:

<cfif NOT isDefined("application.Datasource")>
   <cfset application.Datasource = "MyDatabase">
</cfif>
  • + Share This
  • 🔖 Save To Your Account