CF Coding Practices
  • General Coding Practices
  • Debugging JSON-based CFC Methods
    • Valid JSON Formatting
    • Validating JSON
    • Final Hints
  • How We Invoke Modals
  • Submit Handlers
  • Useful SQL Snippets
  • jQuery/JS How-Tos And Hints
  • jQuery dataTables Tricks
  • CFSCRIPT Snippets and Hints
  • StatusPage Integration
  • Windows Server Setup
  • Common CF Formatting Commands
  • CF Snippets & Hints
  • Developing With The Mura Platform
    • ContentRenderer.CFC
    • Iterators
    • Iterating Remote Feeds
    • Components
    • Nested Content and Interactive Page Assembly
    • Modules and Display Objects
Powered by GitBook
On this page

Was this helpful?

General Coding Practices

Here are some generally guidelines that are adopted for consistent code formatting and best practices for ColdFusion Development. Much of this was originally adopted from an Ortus document on GitHub.

Here are some generally guidelines for CF coding best-practices:

  • Components are supposed to be objects and have an identity. Always ask yourself what this component's responsibilities are and how will it interact with its surroundings.

  • Variables pass in and out of components by reference or by value based on the same rules as the rest of CFML. For instance, strings, arrays, numbers, and dates all pass by value, but structures, queries, and all other "complex" objects (including CFC instances) pass by reference.

  • Arrays in ColdFusion pass by value in Adobe ColdFusion, so beware of this behavior as it is not the same as in Java or other CFML engines.

  • Duplicate() and CFWDDX do not work on CFC instances (ColdFusion 7 and below). CFC’s can only be serialized in ColdFusion 8 and with several restrictions. Be careful when serializing objects as the entire object graphs have the potential of being serialized. ColdFusion 9 presents mechanisms to restrict component serializations which helps incredibly.

  • When extending a component outside the base component's package, the sub-component does not inherit packagepermissions -- thus, you cannot call package methods on other CFCs in the package of the base component from the sub-component.

  • You can have a method that has a returnType or an argument that has a type of a base component and return any component that extends that base component. For example, if methodA takes an argument foo of type motorVehicle and you pass foo as an instance of car, which extends motorVehicle then methodA will honor that car is a motorVehicle when doing type checking on the argument foo.

  • The previous concept applies to interfaces and inheritance.

  • Use interfaces when you want to provide clear API definitions that need to be implemented. They can be good documentation tools and provide compile time checks on your code.

  • Use structKeyExists instead of isDefined when checking for existence.

-- DO THIS --
if( structKeyExists( arguments, "car" ) )
if( arguments.exists( "car" ) )

-- NOT THIS --
if( isDefined("arguments.car") )
  • Use cfswitch instead of cfif if there is a specific expression that can be evaluated against and if there will be more than 2 cfelseif clauses. Not only does it provide more readability, but your code will make more sense.

  • Avoid usage of iif if at all possible as it is documented to be slower. However, sometimes it can prove handy. Also, we typically use the abbreviated version of iif: ([condition] ? [true condition] : [else/false condition]) for example: (!len(trim(PlaceData.BFirm)) ? 'selected' : '')

  • Avoid usage of evaluate() expressions. They have to be evaluated by the ColdFusion engine and will always run slower. There are times when they are needed, especially when doing dynamic concatenations, but try to avoid them at all possible.

-- DO THIS --
<cfset value = form[ "field#i#" ]>
-- NOT THIS --
<cfset value = evaluate("form.field#i#")>
  • Use boolean evaluations

-- DO THIS --
<cfif len(firstName)></cfif>
<cfif NOT obj.isEmpty()></cfif>
<cfif query.recordcount></cfif>
<cfif arrayLen(myArray)></cfif>

-- NOT THIS --
<cfif firstName eq ""></cfif>
<cfif obj.isEmpty() eq false></cfif>
<cfif query.recordcount gt 0></cfif>
<cfif arrayLen(myArray) gt 0></cfif>
  • When creating view templates, surround it with cfoutput tag, instead of nesting them all over the place.

-- DO THIS --
<cfoutput>
<html>
<head>
#head#
</head>
<body>
#leftBar#
#content#
#footer#
</body>
</html>
</cfoutput>

-- NOT THIS --
<html>
<head>
<cfoutput>#head#</cfoutput>
</head>
<body>
<cfoutput>#leftBar#</cfoutput>
<cfoutput>#body#</cfoutput>
<cfoutput>#footer#</cfoutput>
</body>
</html>
  • Code for portability. Avoid at all costs on hardcoding paths, urls, file locations, etc. If you are using a framework. If not within a framework context, try to set global variables in a shared scope such as application scope once when the application loads and then just grab settings from it. Always believe that the application locations can change.

NextDebugging JSON-based CFC Methods

Last updated 6 years ago

Was this helpful?