Compound Assignment Operators in ColdFusion

Compound Assignment Operators in ColdFusion
As the ColdFusion community gears up for the release of ColdFusion 9, I'd like to take a step back for a moment to reflect on a great addition to CF 8 that probably gets overlooked quite a bit, since it's not a feature that would draw a lot of attention.

Enter: Compound Assignment Operators!

oooOOoooo... sexy, right?

A little history... Back in the day when I was cutting my teeth on C programming, I was able to manipulate variables with a few shorthand strokes of the keyboard. It was easy; it was elegant. To accomplish the same tasks in CF, I needed to be redundant in my typing. I hated it.

Then, as I was preparing a new write-up in my UDF Series, I realized that I used a compound operator in the function. I started to explain what it was all about and how/why to modify the function if you're still using releases prior to CF8. But, I also realized that I'm long-winded. That means that my explanation turned into a full-blown rant, worthy of its own post. So, here we are.


Let's dig right into some examples to help understand where we were before compound operators.

Here's an example of incrementing a value in CF7 (using cfscript):

myValue = myValue + 5;

...or like this (using tags):

<cfset myValue = myValue + 5>

To accomplish the same task in C (and most other languages):

myValue += 5;

The difference should be pretty obvious. The C style is clean, simple and elegant. CF7 is redundant.

Now, I'll admit that my code isn't always the cleanest or most elegant. But, it really bummed me out that I was required to type the variable name twice in ColdFusion. It may not seem like a big deal, but it all adds up over time when you consider all of the code that you output in a day... a month... a career.

Then comes CF8 and the world (or maybe just me) rejoices!

How it Works

Using a compound operator is so simple that it may not be immediately clear what's happening. In this example, we're using the compound operator "+=" to perform two concurrent actions on myValue; both the + and the =. It's nicely explained by Adobe this way: "The variable on the right is used as both an element in the expression and the result variable. Thus, the expression a += b is equivalent to a = a +b." [source]

Now that we've learned a new trick, we can use it in CF8+ like this:

<cfset myValue += 5>

Ahhh... that's reeeeal nice!

Note that I did use the compound operator in a tag. I've seen 2 other blog posts that explicitly state that you can use these operators only in <cfscript>. I'm not sure if they're outdated or just incorrect, but you can definitely use compound operators within tags. Of course, use within <cfscript> works as well.

Other Operators

In addition to, well, addition... you can also subtract (-=), multiply (*=), and divide (/=). You can also use it for modulus (%=) and concatenation of strings (&=). There's also incrementing & decrementing by 1 (++ and --). That's pretty spiffy.

You should check out the Adobe LiveDocs for a full explanation.

Bottom Line

The addition of Compound Operators brings CF inline with more traditional syntax of other languages. I was glad to discover it in CF8 and I'm hoping to spread the word to anyone who hasn't discovered this little gem.

Has this solution saved you time?

I'm a firm believer in sharing knowledge for the sake of helping someone else. No strings attached. However, we all have bills to pay and hosting to maintain. If you feel like this post has saved you even an hour of development time, would you consider donating that hour to me?

About the Author

Rob has been in web development for over 10 years, 9 of which have been focused on being a ColdFusion Application Developer. Project Management, eCommerce Consulting, and Marketing Consulting are also in the quiver. If you like what I have to say, consider following me on Twitter or reading more about me here: About Rob O'Brien