Search This Blog

Tuesday, October 29, 2013

ColdFusion: Useful Functions for Files and Directories/Folders

This blog entry is actually for myself. Sometimes, I get confused with the function names because the names are similar to each other. Below are the names and descriptions of their outputs.

<!--- Returns an absolute path from logical path (e.g., C:\inetpub\wwwroot\my_app\) --->

<!--- Returns the absolute path of the application base page (e.g., C:\inetpub\wwwroot\my_app\index.cfm) --->

<!--- Returns the absolute path of the page (e.g., C:\inetpub\wwwroot\my_app\views\main\default.cfm) --->

<!--- Returns the directory from an absolute path (e.g., C:\inetpub\wwwroot\my_app\views\main\) --->

<!--- Returns the filename including the extension from an absolute path (e.g., default.cfm) --->
<br />

Thursday, October 24, 2013

ColdFusion: Error Handling

Most of time when we write code, we assume everything works like it should. However in reality, anything could happen. ColdFusion provides a way for us to handle exceptions gracefully. All these tags and the function are very useful, especially when the application interacts with third-party systems.

  • <cftry>...</cftry>
    Wrap code that could potentially cause an exception with this tag.
  • <cfcatch>...</cfcatch>
    This tag is placed inside <cftry> block. If an exception occurs, the tag would catch it; and we could handle it, such as logging the message or giving users a better error message.
  • <cfthrow />
    This tag is placed inside <cftry> block. If something did not work as expected, we could throw a custom exception and let <cfcatch> handles it.
  • <cfrethrow />
    This tag is placed inside nested <cfcatch> block. Nested <cftry>/<cfcatch> block provides a granular way to handle exceptions. If inner <cfcatch> could not handle it, we use <cfrethrow /> to send the exception to its parent.
  • onError()
    This function is placed in Application.cfc. It catches any errors that are not caught by <cftry>/<cfcatch> block. So, whether you implement <cftry>/<cfcatch> block or not in your code, it would be wise that you implement this function.

For more information about how to use those tags and function, please refers to error handling in ColdFusion documentation.

I found the referring document above is not complete. It's missing <cffinally>...</cffinally>. The tag is used to wrap processes that needs to be run before exiting <cftry>...</cftry>. Keep in mind, it is always run whether there is an exception or not. Without <cffinally>...</cffinally>, any code after <cfcatch>...</cfcatch> would cause an exception.

Some tips for writing better code:

  • Implement onError() in Application.cfc
  • Wrap code with <cftry>/<cfcatch>
  • Use <cfthrow /> for creating custom exception
  • Use <cffinally>...</cffinally> for running addtional processes after cfcatch
  • If there is nesting cftry/cfcatch, we can utilize <cfrethrow />

Tuesday, June 4, 2013

ColdFusion: CFTIMER vs. getTickCount()

In the old days, to measure how long a block of ColdFusion code is executed, we would use getTickCount(). Get the tick before and after the code in question; and subtract them. Newer versions of ColdFusion provide an alternative way to do the specified job. Wrap the code with <cftimer>...</cftimer>.
There is a downside using the tag, you will need to Enable Request Debugging Output in CF Admin. I also found that the "type" attribute is required instead of optional as stated on the documentation. It does not give us an error, but it will not show any results.

<cftimer type="outline">

 <cfset i = 1 />
 <cfloop index="i" from="1" to="100">
  <cfset i = i + 1 /> 


NOTE: I knew that we could use i++ or incrementValue(i), but I just wanted to use i=i+1 for clarity. :)

Monday, June 3, 2013

ColdFusion: Logging Errors Using BugLogHQ

I came across a bug/error logger called BugLogHQ. It includes a control panel where user can browse the logs. Setting it up is pretty easy. Follow its documentation page on

A couple tips. This /bugLog/config/buglog-config.xml.cfm where you define the data source name (DSN), database type, username, and password. For security purpose, Personally, I would not recommend that you set username and password on this config file. If you chose to ignore the username and password, you would need to remove username and password attributes in /components/lib/dao/dbDataProvider.cfc. Otherwise, you would get an error. Another issue that I had during installation where I have MS-SQL Server 2008 installed using Windows authentication, this blog is certainly helpful.

Besides BugLogHQ, there are two other ColdFusion-based loggers I found. They are:

Friday, May 31, 2013

ColdFusion: Working with JSON

JSON has been gaining popularity to represent data in JavaScript applications. Below is an example of JSON data with some array and a ColdFusion parse function to convert JSON data to ColdFusion structure.

<cfsavecontent variable="personnel_record">
 "firstname": "John",
 "lastname": "Smith",
 "age": 36,
 "address": {
  "number": "12345",
  "street": "Any Street",
  "city": "Any City",
  "state": "AA",
  "zip": "90876"
 "children": [
   "firstname": "Ian",
   "age": 7,
   "married": false
   "firstname": "Heather",
   "age": 6,
   "married": false

<cfdump var="#deserializeJSON(personnel_record)#" />

Wednesday, May 29, 2013

ColdFusion: Reduce/Suppress Whitespaces

Without implementing proper technique, by default ColdFusion generates unnecessary whitespaces on result page. This will cause the page size bigger and consequently it will take more time to load the page. To suppress the whitespaces, we can enable whitespace management in CF Admin and/or do one of the following techniques:

 Absolutely no output. Not even <cfoutput> nor <cfdump>.
 Require closing tag </cfsilent>.
 One of the attributes in <cfcomponent> and <cffunction>.
 It works like <cfsilent>.
<cfsetting enablecfoutputonly="true">
 Only output things that are wrapped with <cfoutput>. 
 It works with <cfdump>.
 The effect goes to <cfinclude> as well.
 Matching tag, <cfsetting enablecfoutputonly="false"> is highly recommended.
 Closing tag </cfsetting> is optional and does not have any effect.
 Multiple declarations behave like a counter. 

<cfprocessingdirective suppresswhitespace="true">...</cfprocessingdirective >
 It reduces whitespaces, but it is not as effective as <cfsilent> or <cfsetting>.
 <cfoutput> is not required.
 Require closing tag </cfprocessingdirective>.

<!--- BONUS --->
<cfsetting showdebugoutput="false">
 Disable debugging mode when Debugging Settings in CF Admin is enabled.

ColdFusion Whitespace Options

Thursday, January 3, 2013

Service-Oriented Architecture (SOA)

I recently found a well-written article about Service-Oriented Architecture (SOA) from Microsoft. Although it is not directly related to ColdFusion, the article gives a nice guidance on how to architect applications to deal with service-based collaboration. 

Here's the link: