Skip to content
This repository was archived by the owner on Oct 6, 2023. It is now read-only.

Files

Latest commit

 

History

History
212 lines (168 loc) · 4.98 KB

README.md

File metadata and controls

212 lines (168 loc) · 4.98 KB

Coldfusion Style Guide

Table of Contents

Source Code Layout

Nearly everybody is convinced that every style but their own is ugly and unreadable. Leave out the "but their own" and they're probably right...
-- Jerry Coffin (on indentation)

  • Use UTF-8 as the source file encoding. [link]

  • Use one tab per indentation level. [link]

# bad - two tabs
<cffunction name="fooBar">
	<cfargument name="foo">
</cffunction>

# good
<cffunction name="fooBar">
	<cfargument name="foo">
</cffunction>

Syntax

  • Use one space between the end of the line and the self closing tag [link]
# bad - no space
<set name="fooBar" value="#fooBarValue#"/>

# good
<set name="fooBar" value="#fooBarValue#" />
  • Never use self closing tags unless you are coding XML (where they are required)
# bad - do not use self closing tags in cfm or cfc files
<cfset variables.fooBar='I am located in a cfm file' />

# good
<cfset variables.fooBar='I am located in a cfm file'>
  • No spaces between assigning operators. [link]
# bad - using spaces
<cfset test = "this is a variable">

# good
<cfset test="this is a variable">
  •  Use camelCase for variables, functions, and argument names. [link]
# bad - Function/argument with SnakeCase
<cffunction name="Init">
	<cfargument name="MyObjectId">
</cffunction>

# good
<cffunction name="init">
	<cfargument name="myObjectID">
	...
</cffunction>

# bad - `Id` should be uppercase
<cfset variables.websiteId=12345>

# good
<cfset variables.websiteID=12345>

# bad - Acronyms at the beginning of a variable should be all lowercase
<cfset variables.POSProfileID=12345>

# good
<cfset variables.posProfileID=12345>

# bad - Acronyms in the middle of a variable should be all uppercase
<cfset variables.somePosProfileID=12345>

# good
<cfset variables.somePOSProfileID=12345>
  •  Prefer camelCase for resource bundle variables. [link]
# bad - resource bundle with SnakeCase
<cfoutput>
	#request.rb('FooBar')#
</cfoutput>

# good
<cfoutput>
	#request.rb('fooBar')#
</cfoutput>
  •  Use SnakeCase for object names. [link]
# bad - Object with camelCase
<cfcomponent displayname="myObject">

# good
<cfcomponent displayname="MyObject">
  • Prefer using a positive comparison in an if statement rather than a double negative. [link]
# bad - double negative comparison
<cfif fooBarQuery.queryCount neq 0>
</cfif>

# good
<cfif fooBarQuery.queryCount gt 0>
</cfif>
  • Prefer using lowercase english words when comparing multiple variables in a conditional. [link]
# bad - use of && and ||
<cfif (variables.isFooBar && variables.isBarFoo) || variables.isAllTheFooBars>
</cfif>

# good
<cfif (variables.isFooBar and variables.isBarFoo) or variables.isAllTheFooBars>
</cfif>

Strings

  • Prefer string interpolation rather than string concatenation [link]
# bad - string concatenation
<cfset variables.fooBar='The brown fox' & variables.label & 'the fence.'>

# good
<cfset variables.fooBar="The brown fox #variables.label# the fence.">

SQL

  • Use WITH(NOLOCK) on all select statements to prevent locking the database table for others. (MSSQL specific) [link]
# bad - no WITH(NOLOCK)
<cfquery name="fooBar" datasource="#datasource#">
SELECT *
FROM fooBarTable
</cfquery>

# good
<cfquery name="fooBar" datasource="#datasource#">
SELECT *
FROM fooBarTable WITH(NOLOCK)
</cfquery>
  • Uppercase all SQL functions and reserved words. (MSSQL specific) [link]
# bad - all SQL functions/words are lowercase
<cfquery name="fooBar" datasource="#datasource#">
select top 1 *, count(*)
from fooBarTable with(nolock)
where columnName like '%foo%'
	and columnName2 = 'bar'
</cfquery>

# good
<cfquery name="fooBar" datasource="#datasource#">
SELECT TOP 1 *, COUNT(*)
FROM fooBarTable WITH(NOLOCK)
WHERE columnName LIKE '%foo%'
	AND columnName2 = 'bar'
</cfquery>