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
package
permissions -- thus, you cannot callpackage
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 atype
of a base component and return any component that extends that base component. For example, ifmethodA
takes an argumentfoo
of typemotorVehicle
and you passfoo
as an instance ofcar
, which extendsmotorVehicle
thenmethodA
will honor thatcar
is amotorVehicle
when doing type checking on the argumentfoo
.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 ofisDefined
when checking for existence.
Use
cfswitch
instead ofcfif
if there is a specific expression that can be evaluated against and if there will be more than 2cfelseif
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 ofiif
:([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.
Use boolean evaluations
When creating view templates, surround it with
cfoutput
tag, instead of nesting them all over the place.
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.
Last updated
Was this helpful?