Finally, here is a miscellaneous set of tips and tricks to enhance your applications' processing. Try keeping a notebook of your own as you notice what improves performance, security, and scalability in your own development to create your own best practices.
Use <cflock> Around <cfhttp> Calls
The <cfhttp> tag is used in a single thread by ColdFusion. Locking them will help cut down on p-code errors.
Use Conditional Logic Logically
This may sound redundant, but it isn't. Conditional logic is a lot more than a couple of if/else statements. You must distill in your applications the sine qua non of each block, and write your logic from that. You shouldn't have to check for more than two circumstances or current states at any given time, and you can often get away with one. The way you do this is by contextualizing your code. Nest your logic in a thoughtful, mathematical manner, and your pages will fly. They will also be simple to read and debug.
ColdFusion's short circuit logic is an effort at speeding things up. You can do your part.
Copy Session and Application Variables into the Request Scope
It is imperative that you write <cflock>s around references to session and application variables. So, instead of having to make sure that you've locked everything in all of the many places where you might refer to variables in both of these scopes, copy them to the REQUEST scope. In application.cfm, write this:
<CFLOCK TYPE="ReadOnly" SCOPE="Application" TIMEOUT=30> <CFSET Request.App = Duplicate(APPLICATION)> </CFLOCK> <CFLOCK TYPE="ReadOnly" SCOPE="Session" TIMEOUT=30> <CFSET Request.Ses = Duplicate(SESSION)> </CFLOCK>
What you're doing is copying the structure that holds all of the application variables into the request scope, and then doing the same for session variables. Because you do it in application.cfm, you can reference these variables without having to lock them. But they act exactly the same way (as long as you copy any changes back again into the application and session scopes at the end of the page). Say that you have a database that you set in application.cfm to be your global datasource, like this:
<cflock type="EXCLUSIVE" timeout="30" throwontimeout="Yes"> <cfset Application.Datasource = "MyDatabase"> </cflock>
Then, when you later try to refer to it, you've got to lock that, too:
<cflock type="READONLY" timeout="10" throwontimeout="Yes"> <cfquery name="getUsers" datasource="#Application.Datasource#"> </cflock>
This can get silly after a while.
Using the above trick, you can just refer to your variables like this instead:
<cfquery name="getUsers" datasource="#Request.App.Datasource#">
Note that you can also use the function StructCopy(APPLICATION) to accomplish the same thing, if you prefer. And remember that you have to copy any potentially changed variables into their proper scope once you've had your way with them in your page. So, in OnRequestEnd.cfm, I usually do this:
<cflock name="RequestToApp" timeout="20" throwontimeout="Yes" type="READONLY"> <cfset Application = StructCopy (Request.App)> </cflock>
My new book, Core ColdFusion 5, gives you the nitty-gritty on all the aspects of ColdFusion development that you need to succeed. It contains a wealth of complete applications, including an e-commerce site and many more best practices tips. Pick up a copy today.