Web DevCenter
oreilly.comSafari Books Online.Conferences.
MySQL Conference and Expo April 14-17, 2008, Santa Clara, CA

Sponsored Developer Resources

Web Columns
Adobe GoLive
Essential JavaScript

Web Topics
All Articles
Scripting Languages

Atom 1.0 Feed RSS 1.0 Feed RSS 2.0 Feed

Learning Lab

Top Ten Tips for Developing ColdFusion Components
Pages: 1, 2, 3

4. Get in the Habit of Using Optional Attributes

The tags that are used to write CFCs--cfcomponent, cfproperty, cffunction, and cfargument--each have optional attributes that you don't have to include if you don't want to. Although it's often tempting to leave them off and just accept the default values, you may want to think twice about doing so.

Suffice it to say, the more attributes you specify in your cfcomponent, cfproperty, cffunction, and cfargument tags, the better the auto-generated CFC documentation will be, and the easier it will be to tell what a specific component does. Besides the standard attributes, you can pass any additional arbitrary attributes to the tag as user-defined attributes. Although these attributes don't do anything, they are available as metadata when the component is introspected using the getMetaData() function. You may use any attribute names you wish, with the exception of those reserved by the various CFC tags.

Many of the optional attributes, especially for the cffunction tag, can have a big impact on the behavior of the component method. For example, the access attribute of the cffunction tag determines what code can access the method. The default is public, meaning any .cfm template or CFC on the server can access the method. If you want to expose the method as a web service, you'll need to change access to remote. Another important optional attribute of the cffunction tag is returntype. This attribute specifies the data type of any value returned by the method. If you omit this attribute, it defaults to any. If you specify a value for this attribute, however, you can be assured that the method will only return the type of value you specify. Further, if you design your method to not return any value at all, you can set returntype="void".

These are just some examples of the optional attributes available to the various tags that are used in CFCs. The bottom line is that getting in the habit of investing a little extra time specifying optional attributes now will save you a lot more time in the long run.

5. Use Shared Variable Scopes to Improve Performance

If your application makes extensive use of CFCs, it makes sense that you would want to avoid the overhead of repeatedly instantiating the various CFCs as a user navigates through the application. Likewise, if you build an e-commerce type of application that uses a shopping cart, you would most likely want an instance of the cart CFC for each user. In both of these cases, it makes sense to store instances of your CFCs in a shared scope. In ColdFusion MX, it's possible to assign CFC instances to the server, the application, and the session scopes. Here's an example of how you can instantiate a CFC, in this case a shopping cart, and assign it to the session scope (place this code in your Application.cfm file.):

<cfif not isDefined('session.shoppingCart')>
  <cfset session.shoppingCart = 

And this is the init() method within your shoppingCart.cfc:

<cfcomponent displayname="shoppingCart">
  <cffunction name="init" access="public" output="no" returntype="shoppingCart">
    <cfargument name="cartID" type="UUID" required="yes">
    <!--- Putting the variable in the variables scope makes it available to
          the init() method as well as all other methods in the CFC and to
          the "pseudo-constructor area --->
    <cfset variables.cartID = arguments.cartID>
    <cfreturn this>

If you look at the cfreturn tag, you'll notice that we return this. This is special in that what we returned is an instance of the component.

6. Watch Optional Arguments

If you plan to expose your CFCs as web services, be careful with optional arguments. The Apache Axis 1.1 web services engine used by ColdFusion MX 6.1 does not support optional input arguments. If your CFC methods contain optional arguments, the required attribute setting is ignored, and the argument is treated as required. Here's an example:

  <cffunction name="getMessage" access="remote" returntype="string" output="no">
    <cfargument name="name" type="string" required="no" default="Stinky">
    <cfreturn "Hello " & arguments.name &"! " & "I've been invoked as a web 

In this case, even though name is an optional argument as far as the CFC is concerned, it is required when you expose it as a web service. If you try to call the component method as a web service and don't provide a name argument, ColdFusion throws an error.

7. Suppress Whitespace

If you are concerned about unnecessary whitespace in the output generated by ColdFusion (and you should be), there is a simple step you can take to reduce the amount generated by your CFCs. Both the cffunction tag and the cfcomponent tag have an optional Boolean attribute called output that determines how both output and whitespace are handled:

<cfcomponent output="no">
  <cffuncfion name="foo" output="no">

In the case of the cfcomponent tag, the output attribute indicates whether the component's "pseudo-constructor" code (any area within the cfcomponent container that is outside of a cffunction container) allows output. If yes, expressions (surrounded with pound (#) signs) are automatically evaluated without the need for cfoutput tags within component methods, and the method is permitted to display output. If no, ColdFusion suppresses any output within the constructor. If output is not specified, ColdFusion allows output within the constructor, but all expressions must be within cfoutput tags. For the cffunction tag, the output attribute works the same way, except that it applies only to the area within the cffunction container.

In terms of whitespace generation, unless your constructor area/method will be generating output, always set output="no" to avoid generating unnecessary whitespace.

Pages: 1, 2, 3

Next Pagearrow