Employing Effective ColdFusion Coding


There are many strategies that you can employ to improve the performance of Web applications. The first step is writing code that runs efficiently. Tuning the server and caching are also effective, but they are not cure-alls for poorly written code. This chapter describes techniques you can use to increase the performance of ColdFusion applications. Remember that ColdFusion is like any other language: You can write code that is efficient and performs well, or you can write sloppy code that performs poorly.

Avoiding Nesting <cfif>, <cfcase>, and <cfloop>

Although nesting is sometimes critical in implementing an algorithm, nested <cfif>, <cfcase>, and <cfloop> tags take a toll on processing. As a rule, do not nest deeper than a few levels.

Using <cfcase> Versus Multiple <cfelseif> Tags

Rather than using a number of <cfelseif> tags in a conditional statement, use the <cfswitch>/<cfcase> tag set if possible. However, keep in mind that <cfcase> tests for multiple values of the same expression, while <cfif> / <cfelseif> tests for multiple conditions.

For exact syntax and more information on conditional statements, see Chapter 3, "Conditional Processing."


Optimizing Conditional Expressions in <cfif>

When evaluating expressions, it often is not necessary to use logical operators in <cfif> statements. For instance, you might use the ListFind() function to see whether an element appears in a list. If it does not, the function returns 0; otherwise, it returns the index position of the element. The conditional expression can check whether the returned value is 0, as follows:

 <CFIF ListFind(TheList,"Testing") IS NOT 0> 

You don't actually need to use IS NOT 0 because the value returned by the function is inherently true or false. If the function returns any number besides 0, the value is true, and the comparison using the operator is not necessary.

ColdFusion supports shortcut Boolean evaluations. You should take advantage of this type of evaluation when writing conditional expressions. For example, if you're checking for the existence of a variable and then for a certain value, use the IsDefined() function, then use a logical AND with the next check. If the existence test is false, ColdFusion will know that the result will be false when it sees the logical AND, and it stops the evaluation of the expression.

Avoiding Unnecessary Dynamic Expression Evaluation

The Evaluate(), DE(), and IIf() functions permit you to write highly flexible and dynamic code, but they are processing-intensive and should not be used unless they are truly needed.

You can find more information about the evaluation of dynamic expressions in Chapter 22, "Dynamic Functions."


Reusing Code: <cfinclude> Versus Custom Tags And CFCs

Reusing code with <cfinclude> incurs almost no additional processing time, and the code executes quicker than Custom Tags and ColdFusion Components. For simple processing, the use of <cfinclude> may be preferable.

The <cfinclude> tag is discussed in Chapter 5, "Redirects and Reuse." Custom Tags are covered in Chapter 28, "Custom Tags." ColdFusion Components are reviewed in Chapter 31, "ColdFusion Components."


Typing Variables When Performance Counts

ColdFusion is a typeless language, which can have a small impact on performance. To compensate for this, you could use the <CFPARAM> tag's TYPE attribute when assigning values to help performance. Consider the following:

 <cfif TheVar IS 10> 

If this condition existed, it would be evaluated marginally faster if TheVar were assigned a value, by using:

 <cfparam name="TheVar" default="10" type="Numeric"> 

rather than

 <cfset TheVar=10> 

NOTE

As mentioned previously in this chapter, specifying type has a minimal impact on performance. The real benefit of using <cfparam> with the type attribute is error-checking.


Using Variable Prefixes

If you do not use variable prefixes, ColdFusion must scan a list of variable scopes in a predetermined order so as to find the specified variable. When you use a prefix, the variable's value can be retrieved directly, which saves processing time. However, hard-coding a specific scope can get in the way of implementing flexible code.

See Chapter 2, "Working with Variables and Expressions," for a full discussion of variable prefixes.




Macromedia ColdFusion MX 7 Certified Developer Study Guide
Macromedia ColdFusion MX 7 Certified Developer Study Guide
ISBN: 0321330110
EAN: 2147483647
Year: 2004
Pages: 389
Authors: Ben Forta

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net