ColdFusion 8-Enhancements You May Have Missed

0
82
4 min read

<cfscript> Enhancements

Poor <cfscript>! It can’t be easy being the younger sibling to CFML tags. Natively, you can just do more with tags. Tags are arguably easier to learn and read, especially for beginners. Yet, since its introduction in ColdFusion 4.0, <cfscript> has dutifully done its job while getting none, or little, of the love.

Given that ColdFusion was marketed as an easy-to-learn tag-based language that could be adopted by non-programmers who were only familiar with HTML, why did Allaire make the effort to introduce <cfscript>? Perhaps it was an effort to add a sense of legitimacy for those who didn’t view a tag-based language as a true language. Perhaps it was a matter of trying to appeal to more seasoned developers as well as beginners. In either case, <cfscript> wasn’t without some serious limitations that prevented it from gaining widespread acceptance.

For example, while it boasted an ECMAScript-like syntax, which perhaps would have made it attractive to JavaScript developers, it was tied tightly enough to CFML that it used CFML operators. If you were used to writing the following to loop over an array in JavaScript:

for (var i=0; i<myArray.length; i++) {

… it wasn’t quite a natural progression to write the same loop in cfscript:

for (i=1; i lt arrayLen(myArray); i=i+1) {

On the surface, it may look similar enough. But there are a few significant differences. First, the use of “lt” to represent the traditional “<” (‘less than’ operator). Second, the lack of a built-in increment operator. While ColdFusion does have a built-in incrementValue() function, that doesn’t really do much to bridge the gap between <cfscript> and ECMAScript.

When you’re used to using traditional comparison operators in a scripting language (<, =, >, etc), as well as using increment operators (++), you would likely end up losing more time than you’d save in <cfscript>. Why? Because chances are that you’d type out the loop using the traditional comparison operators, run your code, see the error, smack your forehead, modify the code, and repeat. Well, your forehead is going to love this. As of ColdFusion 8, cfscript supports all of the traditional comparison operators (<, <=, ==, !=, =>, >). In addition, both <cfscript> and CFML support the following operators as of ColdFusion 8:

Operator

Name

ColdFusion Pre CF 8

ColdFusion 8

++

Increment

i=i+1

i++

Decrement

i=i-1

i–

%

Modulus

x = a mod b

x = a%b

+=

Compound Addition

x = x + y

x += y

-=

Compound Subtraction

x = x – y

x -= y

*=

Compound Multiplication

x = x * y

x *= y

/=

Compound Division

x = x / y

x /= y

%=

Compound Modulus

x = x mod y

x %= y

&=

Compound Concatenation

(Strings)

str = “abc”;

str = str & “def”;

str = “abc”;

str &= “def”;

&&

Logical And

if (x eq 1) and (y eq 2)

if (x == 1) && (y == 2)

||

Logical Or

if (x eq 1) or (y eq 2)

if (x == 1) || (y == 2)

!

Logical Complement

if (x neq y)

if (! x == y)

 

For people who bounce back and forth between ColdFusion and languages like JavaScript or ActionScript, this should make the transitions significantly less jarring.

Array and Structure Enhancements

Arrays and structures are powerful constructs within the world of programming. While the naming conventions may be different, they exist in virtually every language. Creating even a moderately complex application without them would be an unpleasant experience to say the least. Hopefully you’re already putting them to use. If you are, your life just got a little bit easier.

Creating Arrays

One of the perceived drawbacks to a tag-based language like CFML is that it can be a bit verbose. Consider the relatively straightforward task of creating an array and populating it with a small amount of data:

<cfset myArray  = arrayNew(1) />
<cfset myArray[1] = "Moe" />
<cfset myArray[2] = "Larry" />
<cfset myArray[3] = "Curly" />

In <cfscript> it gets a little bit better by cutting out some of the redundancy of the <cfset> tags:

<cfscript>
myArray  = arrayNew(1);
myArray[1] = "Moe";
myArray[2] = "Larry";
myArray[3] = "Curly";
</cfscript>

A little bit better. But if you’re familiar with languages like JavaScript, ActionScript, Java, or others, you know that this can still be improved upon. That’s exactly what Adobe’s done with ColdFusion 8. ColdFusion 8 introduces shorthand notation for the creation of arrays.

<cfset myArray = [] />

The code above will create an empty array. In and of itself, this doesn’t seem like a tremendous time saver. But, what if you could create the array and populate it at the same time?

<cfset myArray = ["Larry", "Moe", "Curly"] />

The square brackets tell ColdFusion that you’re creating an array. Inside the square brackets, a comma-delimited list populates the array. One caveat to be aware of is that ColdFusion has never taken much of a liking to empty list elements. The following will throw an error:

<cfset myArray = ["Larry", , "Curly"] /> <!-- don't do this -->

If you’re populating your array dynamically, take steps to ensure that there are no empty elements in the list.

 


 

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here