andyMatthews.net

ColdFusion 9 and pure CFScript CFCs

If you came to ColdFusion from a Java, C++, JavaScript, Actionscript, or any other script based background you might not be aware that ColdFusion has a completely separate syntax. It looks very similar to those other languages but has always been sort of a bastard step-child of the tag based syntax. It wasn't as full featured, and wasn't talked about much. With the release of ColdFusion 9 in late 2009 Adobe made a concerted effort to bring CFScript syntax up to par with tag-based. One of the primary focus points was allowing developers to write ColdFusion Components in pure script. As part of my recent pastebinCFC project, I made the decision to write this new CFC in full CFscript. I wanted to take a short blog post to point out some of the things that I discovered while writing this project.

To start with, CFScript CFCs are much more terse than their tag based brethren. Less code means less typing, which converts to working faster and more efficiently. Let's compare an uber simple tag based CFC with one that's writte in script. Well use a simple bean object which is commonly used in MVC frameworks to store encapsulated data and pass it around.

Update: Dan Skaggs sent over a simpler version of the tag based CFC. I'm adding it here inline. Thanks Dan.

Now let's look at the same CFC written in script, with the same functionality.

Whoa, whachoo talking about Willis? It's less than half the lines of code, and I don't even have method definitions in there for the getters and setters! How's that supposed to work? Well, for starters, in CF9, you can use property to imply properties that comprise your object. In this case we have a string property called username, and another called password. Not only does that define, and create variables for us, but it also type checks them. And with CF9 the ColdFusion engineering team added support for something called implicit getters and setters. This takes every property defined in the object and "writes" virtual methods FOR you. You'd use them the same way.

Not only does it allow me to streamline my code, but you'll notice that when I create a new instance of my object, I don't even have to call the init method. ColdFusion will look for a method named init, and fire it if it exists.

Hope this helps you move forward in your usage of ColdFusion.