Coldfusion Optimization

This Coldfusion Optimization guide is a work in progress document based on research and experience.

Boolean variables will only work reliably in conditional statements if their value equals “true”, “false”, 1, or 0. Type coercision from 1 or 0 is not consistent, so check against “not false” or “GT 0″ explicitly.

Use cfswitch instead of cfelseif branches for more than three conditions

Use cflocation with a reference to a serialized data file or URL parameters to pass data from an action file to a display file instead of cfinclude. This will redirect the browser and append the action page to the browser history. This prevents multiple attempts to update to the database if the user refreshes the display page, which would occur if a cfinclude was used.

Immediately read, parse, and delete uploaded files after inserting validated data into the database to prevent DOS attacks

Use <cfsetting enablecfoutputonly=”Yes”> to suppress whitespace, since nothing gets into the output except what’s between a cfoutput tag, plus it is automatically applied throughout the codebase, and can be turned off by simply changing its value, foregoing the need to delete or comment out a start tag and its closing tag

Do not add a trailing slash in the definition of the configuration parameter in application.cfm such as the document root or a base URL, since it is easier and more readable to use a slash after referencing the variable, and will prevent double slashes from causing path not found errors.

Use Len(CGI.xyz) GT 0 instead of IsDefined(“CGI.xyz”) to check whether the length of a CGI parameter is greater than 0 instead of checking for its existence, since different Web servers support different CGI environment variables, and CGI.xxx is always defined, regardless of its value

Limit concurrent updates to the database using CFTRANSACTION, not CFLOCK. CFTRANSACTION
will affect all accesses to the data being updated, not just those occurring in your ColdFusion code.

Use IsNumeric to distinguish between the number zero and an empty string for type checking conditional statements

Use a blank onrequestend.cfm to pair with application.cfm and speed up processing

Put cfoutput tags outside of loops to avoid unnecessary processing overhead

Use the maxrows attribute in cfquery tags when retrieving a known number of rows

Use init methods for components to pass in default values for public parameters that will be used in multiple methods

Use cfscript to initialize a group of variables instead of multiple cfset statements

Use the length function instead of isdefined to check for existence

Check for existence to avoid initializing variables more than once

Always use cfargument in component definitions and specify whether the required attribute is true or false unless a default value is expected

Use structKeyExists(arguments, “arg_name”) to test for the defined arguments with no default or required value

Always define an init method for components and set var scope for cfc variables

Include the encoding description <cfprocessingdirective pageEncoding=”utf-8″ /> at the top of each page

Use compareNoCase(string1, string2) NEQ 0 instead of string1 IS NOT/NEQ string2

Use listFind instead of the OR operator

Use len or isdefined with a scope specified to check for the existence of variables

Put series of string or numeric data in an array instead of a list for operations other than loops

Always use cfqueryparam and blockFactor(calculated by: 3200/ total byte size of one row) in cfquery tags

Never use the evaluate, incrementValue, or iif functions

Use struct[key] instead of structFind(struct, key)

Use the val() function to ensure the validity of a number before inserting it into a database

Use the trim() funtion to remove whitespace from a string before inserting it into a database

Use a cfc, cfimport, cfinclude, or custom tag instead of the cfmodule tag

Use cfsavecontent instead of the cfcache tag to cache dynamic content

Use cookies and URL parameters instead of passing local variables to check the state

Never use pound symbols on variables inside of a cfset, cfif, or cfscript tag

Use query of queries to loop or join cached queries. This enables pagination without multiple database queries. Check session or cookie authorization when accessing cached data

Use cflock when setting application or session variables

Use session-scoped locks for session variables and named locks for application and server variables.

Initialize global component objects once as application variables using the Singleton pattern instead of creating new instances for each part of the application

Use custom tags for reusable UI widgets, closures, and recursive code that generates dynamic content

Use cfincludes for page specific code that needs to reference application and local scope parameters implicitly

Use components for secure domain specific data, web services, encapsulating business logic, and sharing common functions through a single library

Use user defined functions for complex algorithms and procedures that can be used in multiple instances

References

CFML Coding Standards

Coldfusion Optimization Tips

Coldfusion Design Patterns

ColdFusion Singletons

Coldfusion Web Services

Caching CFCs

Using Variables

Using Structs

Copying Structs

Data Table Widget

Image Gallery Widget

Glossary Widget

Design Patterns in Coldfusion: Composition

Design Patterns in Coldfusion: Iteration

Design Patterns in Coldfusion: Facade

Design Patterns in ColdFusion: Strategy Pattern

Design Patterns in ColdFusion: Template Method Pattern

Design Patterns in ColdFusion: Decorator Pattern

Design Patterns in ColdFusion: Creational Patterns

Design Patterns in ColdFusion: Singleton Pattern

Design Patterns in ColdFusion: Command Pattern

An introduction to ColdFusion Frameworks

Simple Caching Techniques with cfsavecontent

Event Model Programming with Coldfusion

Consuming Web Services

Using CFML without a Coldfusion Server

Performing ColdFusion Processing after a cflocation Tag

Mixins and Coldfusion Components

Private Method Injection

Determining which Function called another Function using Coldfusion

Coldfusion Equivalents to find in Scripting Languages

CFError
CFCache
CFDirectory
CFTransaction
CFMailparam
CFPop
CFStoredProc
CFTransaction
CFImage
IsDate Fuction
IsLeapYear Function
IsBinary Function
IsSimpleValue Function
YesNoFormat Function
XMLFormat Function
ToBase64 Function
ValueList Function
Ceiling Function
FormatBaseN Function
FindOneOf Function
GetToken Function
JSStringFormat Function
StripCR Finction
Mid Function
ExpandPath Function
FileExists Function
DirectoryExists Function
GetFileFromPath Function
GetDirectoryFromPath Function
Server.ColdFusion Property Variable

http://www.adobe.com/devnet/coldfusion/cfml.html
http://gotcfm.com/
http://www.coldfusionsites.com/
http://www.sitepoint.com/blogs/category/tech/coldfusion/
http://net.tutsplus.com/tutorials/other/rapid-development-with-coldfusion-and-cfml/
http://www.cfeclipse.org/
http://www.cffaq.com
http://www.opencfml.org
http://www.packtpub.com/article/coldfusion-8-enhancements-you-may-have-missed
http://www.bennadel.com/blog/1422-experimenting-with-flat-file-coldfusion-cfml-caching.htm
http://ontap.riaforge.org/blog/index.cfm/2008/11/30/A-New-Model-for-Caching-in-ColdFusion

TrackBack URI

Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.