July 1, 2008
In keeping with my "back to basics" theme, I wanted to give my opinion on the top 6 functions that developers often overlook. Are these the most important CF functions? No, but, I often see developers using a combination of other functions, inferior functions, or large code blocks to achieve the same results.

6. HTMLEditFormat(): All this function does is escape special HTML characters. Why is this important? Well, I see many sites that will display a user entered form field in their error message. Using HTMLEditFormat() helps to ensure that a malicious user is not injecting code.

5. REFind(): By golly, regular expressions are not that hard. The power that regex unleashes is amazing, especially when coupled with the other available regular expression functions found in ColdFusion. The complex string searches you can produce with REFind() puts find() to shame. Give it a try ;)

4. structKeyExists(): As a best practice, structKeyExists() should always be used as opposed to isDefined(). It is faster. It is safer (think scopes). It is easy.

3. mid(): When performing code reviews, I often see something like this:

// get rid of first and last 3 characters
foo = left(right(bar, len(bar)-3), len(right(bar, len(bar)-3))-3);

instead of simply:

// get rid of first and last 3 characters
foo = mid(bar, 4, len(bar)-6);

While this is a poor example, I've seen mid() not used many, many times.

2. duplicate(): Structures in ColdFusion are passed by reference. This means that if you pass a structure and make changes to it, the changes are replicated back in the original structure. This is a good thing. It can also be a bad thing when you really want a new and totally different structure value. Here is where duplicate() comes in.

1. listAppend() and listQualify() Two list functions actually make my number one spot. I have seen more chunks of code to handle these two functions, simply because developers forget to check the livedocs when dealing with lists (or arrays or structures...). Consider this:

<!--- create an empty list --->
<cfset myList = '' />
<!--- loop over a query and add one columns values to my list --->
<cfloop query="qFoo">
   <cfset myList = myList & qFoo.myCol & ',' />
</cfloop>
<!--- gotta get rid of that last trailing comma --->
<cfset qFoo = left(qFoo, len(qFoo)-1) />

instead of just:

<!--- create an empty list --->
<cfset myList = '' />
<!--- loop over a query and add one columns values to my list --->
<cfloop query="qFoo">
   <cfset myList = listAppend(myList, qFoo.myCol) />
</cfloop>

Also, listQualify() is a forgotten function. Lets say we have a form with a multiselect box, and are searching against a database using an IN clause against the selected value(s). Well, string values need to be qualified with single quotes (') to work in an IN clause. I've seen this (and this assumes they are using listAppend() ;) ):

<cfset tmpValue = '' />
<cfloop list="#form.myVar#" index="i">
   <cfset tmpValue = listAppend(tmpValue, "'" & listGetAt(form.myVar, i) & "'") />
</cfloop>

Wasted cycles. Instead of just using:

<cfset tmpValue = listQualify(form.myVar, "'") />

So, this post isn't really about if these functions make your list or not. What they are meant to really illustrate is that it is high time everyone heads over to the livedocs and review functions that have been added over the last few versions of ColdFusion, or even functions that have been there all along. Then, bookmark the page and refer to it next time you say "Man, there's gotta be an easier way to perform this simple task", because there just may be!

BTW, if I were to pick my top few underused CF tags, they would have to include:

CFTRY/CFCATCH/CFTHROW
CFSWITCH/CFCASE
CFOBJECT (when calling multiple functions in the same CFC with CFINVOKE, although I prefer createObject() )
CFTRANSACTION

June 29, 2008
I am giving a presentation to a ColdFusion User Group tomorrow on CFCs (ColdFusion Components), and I wanted to quickly post some thoughts.

I follow the CF-Talk lists regularly, and see many conversations about CFCs. The good things about this is that more people are digging their claws into the power that are CFCs. The bad thing about this is that people are getting into CFCs about 6 years after they were introduced. This could be for many reasons (and more):

1. New to ColdFusion
2. Just upgraded from a pre-MX version
3. Just plain comfortable with procedural inline coding.
4. ColdFusion is only a small aspect of the job.

CFCs are a great way to maximize code reuse and maintainability across your application, as well as across other apps. They also enable you to build web services, manage events, extend your code, etc. Really, they are an essential part of any CF developer's toolkit, whether or not you intend to use them for writing Object Oriented CF, or just to organize your code for improved reuse and maintainability.

My challenge to new, existing, whomever developers this week is to learn these four tags. When coupled with CFOBJECT and CFINVOKE, or createObject(), these four tags will make you a pro at CFCs no time!

1. CFCOMPONENT
2. CFFUNCTION
3. CFARGUMENT
4. CFRETURN

These tags can be found for MX, MX7 and CF8 in the appropriate livedocs. Google for "livedocs" and your CF version number to get started.

June 27, 2008
Are you using custom events in JavaScript? If not, they are something you should really look at, especially if you are building JS heavy applications, or building applications using Ajax.

I'm pretty sure that most developers are familiar with JavaScript events. The basics and terminology are that an event is fired when a specific action is taken within your DOM. Listeners subscribe to an event and wait for the event to fire, and when it does, they call a bit of JavaScript or a complete JS function. Common events include click, submit, and mouseover. In HTML, we can create listeners for these events directly through HTML tags using tag attributes. An example of this is the "onclick" attribute of the button tag. Click the following button to see an example:

Example 1 Code:

<button id="myBtn" onclick="alert('howdy');">Click Me</button>

Note that in the "onclick" attribute, we do not have to use "javascript:alert('howdy');" as we would within an "href" attribute, as it is implied that event attribute values will be JavaScript.

The standard JavaScript events are great, but what if you need to define and subscribe to your own events? With the Yahoo! User Interface (YUI) libraries, this is really simple. Why would you need a custom event? Well, tons of reasons. A recent example I needed was to trap ColdFusion errors being returned via an Ajax call to CF. Since the CF error is returned as HTML, just viewing the serialized return value as plaintext was tedious. By applying a regular expression to the value being returned from my Ajax call to CF which looked for the error, I could fire a custom event that notified a listener that an error was found, and show that error in a pop up window as HTML (which is MUCH easier to read!).

Another Ajax example would be a form submission that checks values server side. The standard JS submit event (usually defined in the "onsubmit" attribute of the form tag) is fired every time a user clicks a submit button. This tells us that the form was submitted, but does not tell us if the server side validations being handled through Ajax were successful. To get around this, in my Ajax callback function I checked to see if the validation was successful, and if so I fired a custom event called submitSuccess. Listening for this event, I could then run the appropriate logic within my Web 2.0 application to let the user know their form was submitted successfully and continue within the app.

The YUI can be downloaded from SourceForge (other JS frameworks also have similar functionality). At a minimum, you will need to include the base Yahoo! library and the Event library. During development, you can use the following code, but I would recommend local files in a production environment:

<!-- Dependency -->
<script type="text/javascript" src="http://yui.yahooapis.com/2.5.2/build/yahoo/yahoo-min.js" ></script>

<!-- Event source file -->
<script type="text/javascript" src="http://yui.yahooapis.com/2.5.2/build/event/event-min.js" ></script>

To create a custom event, simply define it as such:

// create a custom event
myEvent = new YAHOO.util.CustomEvent('myEvent', this);

I generally create a JS object to hold my custom events. It makes keeping track of events and notifying other developers of your events much easier.

Now that you have an event, you must subscribe to it (an event without listeners is like a tree falling in the woods with no one to hear it ;) ). We also need to define a callback function, which is the function that runs when the event is heard:

// define a callback function
function callback (e, args) {
   alert('myEvent Fired! Argument: ' + args[0]);
}

// subscribe to the custom event
myEvent.subscribe(callback);

Notice that two arguments are defined in the callback function. The first is the event object itself. The second is an array of arguments passed from when you fire the event. You do not have to pass any arguments, but in the example below, we pass one string argument when we fire the custom event. To fire the event within your code is also simple:

myEvent.fire('Argument');

Hope this helps someone out there. There is more that you can do with custom events that outside of the scope of this introductory article, so please refer to the YUI Event documentation. I plan to blog a few more tips in the near future relating to the YUI, so if there is anything you've been dying to try that you need an example for, let me know.