New ColdFusion 8 Operators Rock!

Since I first started using ColdFusion (in the late 20th century), I have loved the language. Once of the few things I haven't loved about it, however, is the absence of JavaScript-like operators that so many other languages have.

With ColdFusion 8, those are added in.

I know ColdFusion 8 has been out a while, but I generally write programs with portability in mind, so I write back a couple of versions. A program that I am working on now, however, really justified the use of ColdFusion 8 - mostly for cfzip and onMissingMethod.

One of the things that has long been tedious in ColdFusion is incrementing.

i = i + 1;

This isn't a big deal, but it isn't nearly as clean as it is in many other languages.

i++;

Fortunately, now it is. To decrement by one, just use i--. (more on ++)

Today, I had to do a fair bit of string work. By virtue of ColdFusion's list functions this is often quite easy. Once you get outside of what the list functions can do, however, it again gets more tedious than other languages.

MyString = "";
MyString = MyString & MyOtherString;
MyString = MyString & MyThirdString & crlf;
MyString = MyString & MyFourthString;
MyString = MyString & MyFifthString & crlf;

Now once you imagine using hard-coded values mixed in with variables for a bunch of string concatenation and the whole thing gets pretty messy.

ColdFusion 8 makes it as easy as it should be.

MyString = "";
MyString &= MyOtherString;
MyString &= MyThirdString & crlf;
MyString &= MyFourthString;
MyString &= MyFifthString & crlf;

The same principle can also be applied to math.

a = a + b;
a = a - b;
a = a * b;
a = a / b;
a = a % b;

These operations are easy enough, but if you are performing several operations on the same variable the newer options will be a bit easier to read:

a += b;
a -= b;
a *= b;
a /= b;
a %= b;

None of these changes to the language brings new power to the table, but they all improve the ease with which we can write and read code.

Thanks Adobe! Keep up the good work.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Those are nice. I also really like implicit structure and array creation:

myStruct = {key1 = "val1"};
myArray = ["val1","va12"];

and most of all:

<cfloop array="#myArray#" index="myIndex">
# Posted By Bob Silverberg | 10/24/08 8:42 AM
Indeed! I have been taking advantage of those as well.

I am hoping that implicit array and structure creation is improved a bit in ColdFusion 9 though.

I like array looping, but it doesn't work as I had hoped. Still nice for certain situations though.

I am using file looping extensively on my current project - very nice!
# Posted By Steve Bryant | 10/24/08 10:25 AM
Yeah, really liking the new syntax. Don't forget the logical operators as well such as:

OR = bValue1||bValue2
AND = bValue1&&bValue2
NOT = !bValue
EQ = bValue1==bValue2
# Posted By John Whish | 10/27/08 2:10 AM
i'll be more happy when we ca do:

<cfset a = {key1 = ["val1","val2"], key2={key1=1, key2=2}}
# Posted By dfguy | 10/27/08 7:23 AM
John,

Good point. I haven't tried those yet. Most of them don't look like real time-savers over the existing syntax, but I have always really liked the "!" operator. It is clear and concise.

dfguy,

You can do that in ColdFusion 8.01.

What you can't do is <cfloop collection="#{one=1,two=2}#" item="key">, which would be really nice.
# Posted By Steve Bryant | 10/27/08 8:49 AM
Hi Steve,

You're right it's not much of a time saver.

I tend to use them when I'm writing in script blocks as it just looks right. Having said that the logical operators look really odd in tag based cfml!
# Posted By John Whish | 10/27/08 9:43 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.