|
|
As the co-coordinator of the ColdFusion Common Function Library Project at CFLib.org, I receive a lot of emails asking for advice on user defined function (UDF) best practices in ColdFusion. One thing I've noticed over time is that developers tend to ask many of the same questions, so I've compiled ten of the most frequently asked-about topics into this article. I hope it will help developers who are getting started with user defined functions. If you are already comfortable writing UDFs in ColdFusion 5.0, you may still find a few tips here worth considering in your own development. 1. Choose your function names wiselyA name is a name is a name, right? Not when it comes to UDFs. There are several guidelines you should think about when deciding what to name your function, to avoid both errors and organizational problems.
Following these conventions makes it easier to understand what a UDF does without necessarily having to see the code defining the function. It also makes organizing and finding UDFs much easier. 2. A little documentation goes a long wayLike all code, UDFs beg for documentation. Using a standardized documentation format for your UDFs makes it easy for you and other developers to know exactly what a UDF does without having to look at every line of code to figure out what's going on. It also makes writing the documentation much easier, as the format remains the same for each function you document. One documentation method you might find useful is called UDFDoc. UDFDoc is a standardized documentation format that was adapted for UDFs by Raymond Camden and is modeled after the popular JavaDoc format. It consists of a number of comments that appear before a UDF is defined. Consider the following UDFDoc header created for a UDF called
The first line of text gives a short description of what the function does. Each
In order to make documenting your UDFs using the UDFDoc format even easier, a custom tag called CF_UDFDoc has been made available for free. This tag can parse your CFML templates and generate HTML documentation containing the function name and any required parameters for all UDFs it finds. 3. Always
|
|
Related Reading Programming ColdFusion |
Here are some rules to keep in mind for using Var to initialize variables within a UDF:
Var must be defined at the top of the
function, before any other CFScript statements, and take precedence over
any other variable with the same name, regardless of the variable's
scope.Var follow the same naming rules as other
variables. Additionally, they may not be compound variable names such as
My.Var.Name.Var x=1;Var y="Hello";Var z=ArrayNew(1);Var. This means you can't do things like
Var x;One issue I often see with developers starting their foray into CFScript and UDFs has to do with using switch/case statements to handle decision making. Many developers comfortable with the tag-based switch/case syntax used in CFML find that similarly constructed CFScript switch/case code provides unexpected results. For example, the following tag-based code returns the calendar quarter that the specified date occurs in as a string (1st, 2nd, 3rd, or 4th):
<CFSET TheDate="01/01/2002">
<!--- evaluate quarter --->
<CFSWITCH EXPRESSION="#Quarter(TheDate)#">
<CFCASE VALUE="1">
<CFSET Q="1st">
</CFCASE>
<CFCASE VALUE="2">
<CFSET Q="2nd">
</CFCASE>
<CFCASE VALUE="3">
<CFSET Q="3rd">
</CFCASE>
<CFDEFAULTCASE>
<CFSET Q="4th">
</CFDEFAULTCASE>
</CFSWITCH>
<CFOUTPUT>
#MonthAsString(Month(TheDate))# is in the #Q# quarter of the year.
</CFOUTPUT>
If you run this example, you should see the code return the string:
January is in the 1st quarter of the year.
The same code written as a UDF might look something like this:
<CFSCRIPT>
function QuarterAsString(date){
// assign the numeric quarter associated with
// the passed in date
Var theQuarter = Quarter(date);
Var q=4;
//evaluate the quarter and convert to string
switch(theQuarter){
case 1:
q="1st";
case 2:
q="2nd";
case 3:
q="3rd";
default:
q="4th";
}
return q;
}
</CFSCRIPT>
<CFSET TheDate="01/01/2002">
<CFOUTPUT>
#MonthAsString(Month(TheDate))# is in
the #QuarterAsString(TheDate)# quarter of
the year.
</CFOUTPUT>
If you run this example, the first thing you'll notice is that the output is wrong. The program is trying to tell you that January is in the 4th quarter of the year, which is obviously wrong. What gives? After all, the code looks like it's identical to the tag-based code in the previous example.
The problem has
to do with how ColdFusion's CFScript (and many other languages) deal with
switch/case statements. In order to avoid falling through the
first true case evaluation, and having the next case statement execute
regardless of whether it's true or false, you need to have a break; statement
after each case.
Adding a break; statement after each case is evaluated causes ColdFusion to exit
the switch once a case evaluates as true. Try the following modified example.
Note the presence of a break; statement at the end of each case statement.
<CFSCRIPT>
function QuarterAsString(date){
// assign the numeric quarter associated with
// the passed in date
Var theQuarter = Quarter(date);
Var q=4;
//evaluate the quarter and convert to string
switch(theQuarter){
case 1:
q="1st";
break;
case 2:
q="2nd";
break;
case 3:
q="3rd";
break;
default:
q="4th";
}
return q;
}
</CFSCRIPT>
<CFSET TheDate="01/01/2002">
<CFOUTPUT>
#MonthAsString(Month(TheDate))# is in
the #QuarterAsString(TheDate)# quarter of
the year.
</CFOUTPUT>
Executing this example results in the output you would expect:
January is in the 1st quarter of the year.
Pages: 1, 2 |
|
|
||||||||