To ensure that your application functions and performs as you would expect it to, you'll want to adhere to some coding practices that keep your application in top working order. The following sections touch on some simple things that you can do as a developer to make a big difference in how your application code is maintained.
Comment, Comment, Comment
Commenting code is imperative to creating an application that is easy to maintain. With proper commenting, developers can come and go, but you'll know that the proper inline documentation is there and sufficient to get them up to speed on the purpose of the template and the particulars of smaller code segments.
It is recommended that your comments include a general usage comment at the top of the template. This comment should include the template name, description of its purpose, the author's name and email address, and the date that it was created. The template should also include a revision comment at the bottom of the template that includes the dates of code revisions and the purposes of the modifications.
Of course, you'll want to include inline comments to describe where variables come from, why they are used, and any complex operations that they perform.
Use CFSCRIPT for Long Variable Assignment Blocks
It is always more efficient to use a CFSCRIPT block to set lots of variables rather than a long line of CFSETs. Not only are the blocks more readable and easier to maintain, but a CFSCRIPT block executes more quickly than setting the same number of variables with CFSETs.
If you must set multiple variables, you could use the following syntax:
<cfset variables.name = "Neil Ross"> <cfset variables.city = "Mechanicsburg"> <cfset variables.state = "Pennsylvania"> <cfset variables.email = "email@example.com"> <cfset variables.url = "http://insidecoldfusionmx.com/">
However, this syntax would be more efficient:
<cfscript> variables.name = 'Neil Ross'; variables.city = 'Mechanicsburg'; variables.state = 'Pennsylvania'; variables.email = 'firstname.lastname@example.org'; variables.url = 'http://insidecoldfusionmx.com/'; </cfscript>
Scope All Variables
Make sure that you are adding the proper scoping prefix to all your variables. Doing this makes your code much easier to read and manage. It also helps to cut down on the extra processing time required for ColdFusion to drill down through the various variable scopes. You'll want to avoid problems like this:
<cfscript> firstname = 'Neil'; lastname = 'Ross'; url.firstname = 'Jeff'; url.lastname = 'Howden'; </cfscript> <cfoutput> My name is #firstname# #lastname#, but you can call me #url.firstname# #url.lastname#. </cfoutput>
The code is a whole lot easier to read if you scope the variables properly:
<cfscript> variables.firstname = 'Neil'; variables.lastname = 'Ross'; url.firstname = 'Jeff'; url.lastname = 'Howden'; </cfscript> <cfoutput> My name is #variables.firstname# #variables.lastname#, but you can call me #url.firstname# #url.lastname#. </cfoutput>