This document is (one of many) coding style documents available for ColdFusion on the web. This one combines the best practices of several noted ColdFusion developers into a single cohesive coding style guideline document you can implement immediately with excellent results.
3. File Naming
Suffixes:
HTML files end in .html
CFML files end in .cfm
Component files end in .cfc
XML files end in .xml
CSS files end in .css
JS files end in .js
In general, follow our existing file naming conventions for files: all SE facing URL‐accessible
filenames shall be lowercase, with words separated by underscores (e.g. get_product_list.cfm).
All non SE facing URL‐accessible filenames shall be headlessCamelCase (e.g.
addNewProduct.cfm). Filenames must never contain spaces!
Note: Application.cfc, Application.cfm and OnRequestEnd.cfm are the only exceptions to the lowercase
filename rule for URL‐accessible files and must have exactly the case shown!
ColdFusion Components
The component name shall be headlessCamelCase; all method names, property names and
instance names (variables referring to components) shall be headlessCamelCase. All references to
component names in code shall match exactly the case of the implementation filename, i.e.,
references will be path.to.headlessCamelCase.
Custom Tags
Custom tags should not be used, however in the event that you must write one, the names will
be lowercase_words. Their implementation filename should be lowercase_words.cfm, stored
somewhere within the {cfmxroot}/extensions/customtags/ hierarchy (so custom tags cannot be
invoked directly via a URL). They should be invoked using a tag prefix (defined using cfimport
before the first use of any custom tags in each file ‐ cfimport tags should be grouped together
near the top of the file) e.g., <pfx:lowercase_words ...> ... </pfx:lowercase_words>. The pfx will usually
be the lowest‐level directory containing the tags, e.g., mmlf for
{cfmxroot}/extensions/customtags/mmlf/ ‐ used like:
<cfimport taglib="/customtags/mmlf" prefix="mmlf" />
...
<mmlf:ssi virtual="/path/to/file.html" />
The expectation is that directories under the Custom Tag Paths will have unique names ‐ the tag
prefix must be unique within a page.
7. Insert Data qPutQueryname qPutCustomer
Delete Data qDelQueryname qDelCustomer
Comments
This section provides guidelines on commenting your source code. In general, we should
comment code to assist other developers work on it in the future. We do not want our
comments to be visible to the public so we do not want to generate HTML comments from
CFML ‐ we use <!‐‐‐ ... ‐‐‐> in CFML which does not get published into the HTML. This means that
for file types that can be accessed directly over the web, such as JavaScript include files, XML
files and CSS style sheets, we should keep comments to a minimum ‐ documentation for such
files must be maintained separately, in the "projects" area of our internal site for example.
Comments are there to be read ‐ consider your audience!
General Guidelines
Write CFML style <!‐‐‐ ... ‐‐‐> comments, for all important entities, that describe what code does
and why ‐ document the how if it is not obvious.
When you make a change, comment it. Identify the change with the date and your user name:
<!‐‐‐ 2001‐11‐26 springled Expanded the Comments section ‐‐‐>
When you want to leave a note about a bug to be fixed or functionality to be added, put TODO:
in front of the actual comment so developers can easily search for them:
<!‐‐‐ 2001‐11‐26 springled TODO: Incorporate everyone's feedback ‐‐‐>
Additional standard search keywords can be added after TODO: e.g., FIXME:, NOTE: ‐ this is very
important as it helps your audience, other developers. Furthermore, standard tags like this can
be read by code editors such as Eclipse to create a "task list" whenever you're working on a file.
<!‐‐‐ 2001‐11‐26 springled TODO: BUG: Fails on Fridays ‐‐‐>
File Comments
Each CFML file should begin with an CFML style <!‐‐‐ ... ‐‐‐> comment containing the filename and
a standard copyright message followed by an explanation of the file and then, optionally, its
modification history:
9. Put the label text you might show to a business user (on an edit form) in displayName (if
the display name and the actual name are the same, you can omit displayName=).
Put a sentence or two of usage details in the hint attribute. A good, readable style is to
use first person narrative to describe things, e.g., a User cfcomponent tag might have hint="I
represent a user who is browsing the members‐only part of the site", a getName() method in that
component might have hint="I return the full name of this user as 'Firstname Lastname'" and the
argument for the read() method of a UserDAO component might have hint="I am a UUID that
uniquely identifies a user within the membership database".
Note: The displayName and hint 'comments' are in addition to the file comment described above.
HTML & XHTML Compliance
All generated HTML should be XHTML‐ready:
Lowercase element and attribute names,
All attribute values in quotation marks,
Close all tags correctly, e.g., close <p> with </p> and <li> with </li>,
Although there are rumors that closing empty‐body tags with /> breaks some old
browsers, it is still recommended to do this, e.g., <br /> (note the space before the /),
<img src="..." ... />. Set Dreamweaver to generate XHTML instead of HTML.
Generated HTML that is XHTML‐compliant should begin with this:
<?xml version="1.0" encoding="utf‐8"?>
<!DOCTYPE html PUBLIC "‐//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1‐transitional.dtd">
Some browsers interpret this as strict XHTML so you also need to add the following to the HTML
tag:
<html xmlns="http://www.w3.org/1999/xhtml">
Do not put the above code in generated HTML that is not XHTML‐compliant!
CFML & XHTML Compliance
ColdFusion source code cannot quite be written to be purely XHTML‐compliant because of
certain tags (cfif / cfelse, cfreturn, cfset) but you should make an effort to be as XHTML‐compliant
as possible. cfelse cannot have a closing tag so it cannot be XHTML‐compliant; cfif and cfreturn do
not have an attribute="value" syntax so they cannot be XHTML‐compliant (but cfif has a closing /cfif
tag and cfreturn can and should have a self‐closing /); cfset does not in general follow the
attribute="value" syntax and these guidelines recommend that for readability you do not quote
the value in cfset ‐ but cfset can and should have a self‐closing /. This makes the source code
13.
Example 3 (Insert type B):
INSERT INTO TABLE_ONE (COLUMN_ONE, COLUMN_TWO, COLUMN_THREE)
VALUES ('ValueOne', 'ValueTwo', 'ValueThree')
Example 4:
UPDATE TABLE_ONE
SET COLUMN_ONE = 'ValueOne', COLUMN_ONE = 'ValueTwo'
WHERE TABLE_ONE_ID = 10 AND COLUMN_THREE = 'ValueThree'
Application.cfm / Application.cfc
There will be a root Application.cfm or Application.cfc file that provides all the site‐wide core
services such as application and session settings, site‐wide constants, form / URL encoding
machinery etc. Values can all be set in request scope rather than variables scope to ensure they
are available inside custom tags etc.
Each "application" on the site should have an independent Application.cfm file (containing all the
appropriate definitions) or an Application.cfc file.
/Application.cfm:
set up application and session settings:
<cfapplication name="<yourAppName>" sessionmanagement="true" .../>
user session setup
process loc= to create request scope language / locale values, as needed
globalization / encoding (always):
<!‐‐‐ Set encoding to UTF‐8. ‐‐‐>
<cfprocessingdirective pageencoding="utf‐8"/>
<cfcontent type="text/html; charset=UTF‐8"/>
<cfset setEncoding("URL", "UTF‐8")/>
<cfset setEncoding("Form", "UTF‐8")/>
include site‐wide and server‐specific constants:
<!‐‐‐ Site‐wide constants ‐‐‐>
<cfset .../>
14. <cfset myVar = “bob”> ‐ sets myVar in VARIABLES scope
<cfset REQUEST.myVar = “bob”> ‐ sets myVar in the REQUEST scope
<cfset SESSION.myVar = “bob”> ‐ sets myVar in the SESSION scope
<cfset APPLICATION.myVar = “bob”> ‐ sets myVar in the APPLICATION scope
Directory Structure For Applications
All ColdFusion code should live in the root folder of the web server application (wwwroot for
IIS, htdocs for Apache) and we'll refer to that as the {cfmxroot} below:
{cfmxroot}
<sitename>/ » tree for root .cfm files – e.g. www.mydomain.com
components/ » tree for .cfc files
images/ » tree for image files
includes/ » tree for include files
js/ » tree for .js javascript
style/ » tree for .css files
xml/ » tree for .xml files
<dir...> » tree for additional .cfm
Performance "Do's"
The following are 'positive' recommendations, e.g., "Do xyz..." or "Do xyz instead of...".
Use compareNoCase() for comparing two strings
Use compareNoCase() or compare() instead of the is not operator to compare two items. They are
significantly faster. Remember that these functions return 0 if the values match, so they
correspond to is not.
Example: <cfif compareNoCase(x, "a") neq 0>
Not: <cfif x is not "a">
Use listFindNoCase() for OR comparisons
Use listFindNoCase() or listFind() instead of the is and or operators to compare one item to multiple
items. They are much much faster (order of magnitude for 5+ options).
Example: <cfif listFindNoCase("a,b,c", x) is not 0>
Not: <cfif x is "a" or x is "b" or x is "c">
Use arrays instead of lists ‐ in general