Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > Adobe ColdFusion

  • Print
  • + Share This
This chapter is from the book

CFC Good Practices

Now that you have some understanding of ColdFusion Components—how to create and use them, how to extend them with OO concepts, and how to secure them—let's close with a few things that are always good to remember when using CFCs in your applications. Notice the heading for this section doesn't say "Best Practices"—CFCs are going to be used differently by everyone. They are not a strict framework. You may use them as a sort of "next generation" custom tag, or as the foundation of an implementation of an extremely formal OO pattern, or anything in between. Whatever your intent, here are some things that should become habit whenever you use components.

Use Hints

It may seem obvious to you what a particular function does or what the general use is for a component, but it may not be obvious to anyone else! This may be an incorrect attribution, and the quotation marks are mine—but a story goes that T.S. Eliot once gave a lecture toward the end of his life; he opened up the floor to questions. An eager young student began speaking about line four-hundred-something in The Waste Land, where Eliot had written some cryptic phrase; the student asked, "What, exactly, did you mean by that?" Eliot paused and, finally answered, "Young man, when I wrote that line only two people knew what it meant; myself and God. Now...God only knows!"

So use the hint attribute in every <cffunction> and <cfcomponent> tag. Use <cfproperty> to let your users know what properties your component makes available and what they contain. Anyone who reviews the work, including you, will use the CFC's self-documenting feature to much more advantage when brief, concise hints are included.


CFC functions should only have their accesses set as remote if you really intend them to be used remotely. If you are not using SOAP Web Services or Flash Remoting in your application, and you don't intend to call the component directly from a URL, don't use this attribute. If a major point of CFCs is to encapsulate your business logic, then that is precisely the code you don't want to leave open to the "outside world."

Don't Expose Yourself Unless You Need To

Only use the THIS scope inside a component when you intend for your component's properties to be used directly. If you want to hide the details of your component's operation, create a function called getMyName() instead of allowing your components' users to use In this way you avoid allowing code that invokes your component to change its values without your component's being aware of it.

Use Data Types

<cffunction> has the attribute returntype="...", and <cfargument> has the attribute type="..." for setting data types of incoming arguments and outgoing return values. Use them! If you are using the component for SOAP Web Services, you have no choice. Without these attributes set properly, ColdFusion cannot make the WSDL to describe the service, and the Web service consumer will not know how to use it. When ColdFusion is using the component, these data type specifications are not required but are desirable. They will also make the self-documenting view of the component more useful.

For example, this line tells us without confusion that the method requires a numeric argument and returns a query recordset:

query getActorDetail ( required numeric actorID )

Design for Your Project

Don't feel bullied into using one specific design pattern when developing with ColdFusion Components. Each methodology has its own strengths and weaknesses, so feel free to select the methodology that best fits your specific project—or don't choose any! CFCs are flexible for a reason.

Use CFCs!

The best "good practice" on which to end this chapter is simply to use CFCs in all of your applications. There is no application so simple that it would not benefit from the inclusion of components. That one-page, single-access database query, tabular-output application needs a clean separation of logic and presentation as much as the most complex e-commerce site. An application built with CFCs is just as easy to build and will pay you back many times over in maintainability, code reuse, openness, and development team benefits. And all of these benefits come at no additional performance cost over ordinary methods of code reuse/encapsulation. Adobe has published performance numbers that show CFCs, custom tags, and user-defined functions all perform nearly the same.

  • + Share This
  • 🔖 Save To Your Account