top of page
  • Writer's pictureTroy Web Consulting

2 Important (Overlooked) Features Of ColdFusion 9

Recently I was chatting with someone on our dev team and I realized that not everyone is as heads-down, buried in ColdFusion every day like I am. New features come and go (do they go?) and we latch on to what we can, when we need it. But depending on what you were doing back when CF9 released, you may have missed these. Yes. these are old features already (4 years) but real nice features just seem to slip through without being harnessed. these are (2) big deal items I see overlooked that you really should use:


Local Variable In Functions


We all know about using the var keyword to declare local variables. if you don't already know, look it up. In CF8, we had to do all these var declarations at the top of the function. Pain in the ass. someone kick the compiler guy/gal.

Leave it to our CF community to come up with a brilliant solution that is one of the more elegant "standards" I've seen evolve in the CF world (along with init() pseudo-constructors). All ya gotta do is add this code at the top of every function:

var local = {}; // structNew()? get hip, use the squirrelly brackets!

Then, in your function code when you need a local variable, just use local.

what sucked? well, you have to use local. prefixes on all your local variables which is particularly lame with an lcvs (loop control variables), like i,j,k etc... sometimes you'd see folks declare these as local separately just so they could reference it using #i# or the like.

Overall, nice solution. but that was pre 2009; (eh hem, 4 YEARS AGO) - what's the gig with CF9?

First of all, in CF9 we can declare a var anywhere in the function. 'bout time. the compiler guy/gal got the message. but that's only half the story.

Them Adobe folks also made an implicit local scope in all functions, called... (wait for it, wait for it... drumroll) local. Yea, they scarfed the "standard" and rolled it in. nice job Adobe (i don't say those words too often these days, so I had to throw it in). Well, anyway, what are the perks to this new scope? (2) things:

  1. You don't have to declare the local structure at the top of the function. it's already there (but if you do, it's backwards compatible, so no worries)

  2. You don't have to reference the local variables using local. once they are initially set in the local. scope, you just reference them directly and they are found in the scope chain

thus:

for(local.i=1;i<10;i++){ writeOutput(i); // works like a charm }

Implicit setters and setters

Here's one i didn't figure out until i had to teach a class in ColdFusion 9 - ColdFusion allows you to add implicit getters and setters with one easy attribute to the <cfcomponent> tag: accessors="true"

of course Ben Nadel crawled behind the dashboard to check the wiring on this - pretty cool experiment he did too. At the end of the day it's not a huge deal, but here's whay I like it. If forces me to do things "the right way" and it makes it easy. That's pretty much what CF is all about.

Mike Pacella, one of our CF/Java ninjas got me in the habit of using getters() and setters() a lot - it seems a bit uptight sometimes and quite frankly, you dont have to do it for everything - but if your talking about instance properties on an object, well, yeah, you probably should. but it's a hassle to write getters and it feels like a waste of time when those methods just get and set. I write enough code, i dont need to do more. shut up, I know i can use snippets or generators, or whatever. I do. it's still a pain in the ass.

so here's the layup - you add accessors=true to the <cfcomponent tage and all your properties have getters and setters available by default. AH BUT WAIT how the hell does the compiler know what the run-time instance properties will be. this is ColdFusion, we don't declare those things to our compiler. This is the other thing i love about this feature - we actually use <cfproperty and now, its a tag that's actually used by the compiler ISO just being a documentation scrap. I'm a bit of a documentaiton nut, so I like the idea that if I create this tag and thereby write documentation for my doc engine parser, I'm also writing code that is doing something - in this case defining properties for the implicit getters and setters. putting this all togther, here's a little cfc code you might see:

<cfcomponent accessors="true"> <cfproperty name="Globals" type="struct" hint="the global variables, by reference." />

<cffunction name="init" access="public" returntype="obj"> <cfargument name="globals" required="no" default="#{}#" hint="reference to global variables" /> <cfscript> setGlobals(globals); </cfscript> <cfreturn this> </cffunction> </cfcomponent>

so that's it.

bottom of page