I am often conflicted over the use and methodology of cfscript.  According to the ColdFusion MX 7 livedocs site on adobe.com, cfscript is defined as:

CFScript is a language within a language. It is a scripting language that is similar to JavaScript but is simpler to use. Also, unlike JavaScript, CFScript only runs on the ColdFusion server; it does not run on the client system. CFScript code can use all the ColdFusion functions and expressions, and has access to all ColdFusion variables that are available in the script’s scope.

    The article continues to say when it is most appropriate or most efficient to utilize the power of cfscript:

 

CFScript provides a compact and efficient way to write ColdFusion logic. Typical uses of CFScript include the following: * Simplifying and speeding variable setting * Building compact JavaScript-like flow control structures * Creating user-defined functions Because you use functions and expressions directly in CFScript, you do not have to surround each assignment or function in a cfset tag. Also, CFScript assignments are often faster than cfset tags. CFScript provides a set of decision and flow-control structures that are more familiar than ColdFusion tags to most programmers.

 

    I think the most important factor that lends power to the supporters of the use of cfscript is the following statement:

In addition to variable setting, other operations tend to be slightly faster in CFScript than in tags.

    It should also be noted that all the functionality and power of tags cannot be directly achieved through the use of cfscript.  For example, you can create a query using cfquery within a cfscript block; you can, however, instantiate a component and call a method or directly call a user-defined function within the scope of the cfscript tag that will execute the query and return the query object to a variable within your cfscript.

    I find myself often using the power of cfscript when I am setting variables, setting and getting to and from a database using a CFC, and performing large computations and logic on a dataset.  I really like the power of the brackets in cfscript.  Here is a snippet of code from an application that heavily uses cfscript to create a summary view from transactional data:

?View Code COLDFUSION
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
for (k=1;k lte grantdetails.recordcount; k=k+1){
    /*Updated: Only display Accounts whose object code starts with 4, and the source type is not YE*/
    transyear = Left(grantdetails.TransactionDate[k],4);
    transmonth = Mid(grantdetails.TransactionDate[k],5,2);
    transday = Right(grantdetails.TransactionDate[k],2);
    transdate = CreateDate(transyear,transmonth,transday);
    if ((ucase(grantdetails.source[k]) neq "YE") AND (Mid(grantdetails.glnumber[k],10,1) eq "4")){
        if (ucase(grantdetails.sourcetype[k]) eq "A"){
            if (summary.actual[gls[grantdetails.GlNumber[k]]] eq ""){
                QuerySetCell(summary,"actual",grantdetails.amount[k],gls[grantdetails.GlNumber[k]]);
            }else{
                oldamount = summary.actual[gls[grantdetails.GlNumber[k]]];
                newamount = oldamount + grantdetails.amount[k];
                QuerySetCell(summary,"actual",newamount,gls[grantdetails.GlNumber[k]]);
            }
        }else if (ucase(grantdetails.sourcetype[k]) eq "E"){
            //only include encumberances in the current period
            if (currentsdate neq '' AND currentedate neq ''){
                if (DateCompare(transdate,currentsdate) eq 1 AND datecompare(currentedate,transdate) eq 1){
                    if (summary.encumbered[gls[grantdetails.GlNumber[k]]] eq ""){
                        QuerySetCell(summary,"encumbered",grantdetails.amount[k],gls[grantdetails.GlNumber[k]]);
                    }else{
                        oldamount = summary.encumbered[gls[grantdetails.GlNumber[k]]];
                        newamount = oldamount + grantdetails.amount[k];
                        QuerySetCell(summary,"encumbered",newamount,gls[grantdetails.GlNumber[k]]);
                    }
                }
            }
        }
 
    }
}

    I could not even imagine the length and complexity of this code if I was using tags instead of cfscript.  I think this snippet shows the immense power and capabilities of cfscript.  Want more information about cfscript?  Check out the livedocs page entitled: Extending ColdFusion pages with CFML scripting

    What I want to know is: what do you think?  Do you use cfscript or do you just ignore it?

This entry was posted on Monday, April 23rd, 2007 at 2:52 pm.
Categories: Uncategorized.

No Comments, Comment or Ping

Reply to “To cfscript or to cftag?”