The Dopefly Tech Blog

« The Dopefly Tech Blog Main page

12 ways to reuse your ColdFusion code

posted under category: ColdFusion on April 8, 2010 by Nathan

It's no doubt that code reuse is an important part of programming. Here's a list of ways you can do it while you're programming in ColdFusion.

  1. <cfinclude template="file.cfm">
    This is the most basic server-side include, which pulls one file into another. There is no way to send parameters to the included file (besides setting variables before the include). The included template can by dynamic using a #variable#. Unlike classic ASP, the file is included only when the tag executes.

  2. include "file.cfm"
    This is the cfscript version of the cfinclude tag and behaves similarly.

  3. <cf_file attribute1="value1">
    This is Custom Tag syntax. You can use this to send parameters at an included file. In this example, file.cfm exists either in the relative current directory, in a custom tag path specified by your Application.cfc file, or in a custom tag path specified in the CF Administrator.

  4. <cfmodule [name= or template=] >
    Similar to the custom tag model, but you can use any file with a relative path.

  5. <cfimport namespace="imported"> ... <imported:file />
    Specify a directory and namespace, and you can use this JSP style include. The cfimport tag must be applied on every template you wish to use the namespaced file set in. Imported files behave like custom tags in that they use attributes and can have a closing tag, but it can also be used for JSP tagsets.

  6. <cffunction name="reusable"></cffunction>
    Defines a reusable function, method or subroutine of your application. You can type this anywhere in your application, except within another function.

  7. function reusable() {}
    This is the cfscript version of cffunction. Since CF9, it can use extra syntax to give more metadata and control like the tag-based version has.

  8. <cfcomponent> ... </cfcomponent>
    This is the root element of a .cfc file. Any code within is executed immediately upon instantiation (see cfobject). The methods (functions) within are executed upon request.

  9. component { ... }
    This is the cfscript version of defining a CFC. This also provides the only way to write cfscript without the cfscript tags.

  10. <cfobject >
    Creates an instance of (a.k.a. "instantiates") an object, which could be Java, .NET, a web service, or a variety of others, but in this case, we are talking about creating an instance of a ColdFusion Component (CFC) or ColdFusion web service.

  11. createObject("component", "myComponent")
    new myComponent()

    cfscript versions of cfobject and traditionally the easiest way to create an instance of any CFC.

  12. <cfinvoke>
    Invokes a method on an object or webservice, instantiated or not.

That's a big list!

Nathan is a software developer at The Boeing Company in Charleston, SC. He is essentially a big programming nerd. Really, you could say that makes him a nerd among nerds. Aside from making software for the web, he plays with tech toys and likes to think about programming's big picture while speaking at conferences and generally impressing people with massive nerdiness and straight-faced sarcastic humor. Nathan got his programming start writing batch files in DOS. It should go without saying, but these thought and opinions have nothing to do with Boeing in any way.
This blog is also available as an RSS 2.0 feed. Click your heels together and click here to contact Nathan.