CF9 Feature Wish: Properties, please!
Posted by Joe Rinehart at 8:25 AM
17 comments - Categories:
ColdFusion MX
A long time ago, I did C# development. It was brief. I liked the language, but couldn't stand the project. The only good code that came out of it was Model-Glue and the the Alagad Image Component: Doug and I both had a lot of spare time during a 3-hour sync/build process that had to be performed manually.
That aside, one thing I loved about C# were its notion of "property" functions. Instead of writing getFirstname() and setFirstname() functions to hide the "firstname" property, you could simply declare a public firstname property. If you needed to implement data hiding, you could simply change "firstname" to a private mFirstname property and expose the public "firstname" by adding a getter and setter function:
public String firstname {
get {
return mFirstname
}
set {
mFirstname = value;
}
}
ActionScript 3 has similar features. When I model a class, I typically start with all-public properties, shifting to getter/setter functions wrapping a private variable as needed:
public function get firstname():String
{
return _firstname;
}
public function set firstname(value:String):void
{
_firstname = value;
}
Would it be that hard for ColdFusion to do the same? I'm tired of writing getter and setter functions (and writing and maintaining tools that do it for me). Other automagic solutions, such as onMissingMethod and mixins, result in what I call a "squishy" API: you don't know its form until runtime. Hard to document!
Here's something I'd love to see in ColdFusion 9:
...works exactly like now. It declares a property, but doesn't set it, just like "public var firstname:String". Developers work directly with instance.firstname.
When we need to "hide" data and have getter/setter logic applied, we can expand our property:
<cfsetter>
<cfset variables._firstname = value />
</cfsetter>
<cfgetter>
<cfreturn variables._firstname />
</cfgetter>
</cfproperty>
The <cfsetter> tag is basically a <cffunction> tag invoked when a developer performs an assignment to firstname, receiving a single argument named "value." When the firstname property is accessed, the <cfgetter> function is invoked.
It'd require changes to the "under the hood" mechanics of accessing public-facing members of CFC instances, but I think it'd be worth it.
I've sent along a feature request to Adobe asking for this. If you'd like to do the same, visit the feature request form. If you'd like to do it quickly, without typing your own feature request in, simply visit the form, change "This is a:" to "Feature Request," and paste the following text into the text area describing this feature:
private _firstname:String;
public function get firstname():String { return _firstname; }
public function set firstname(value:String):void { _firstname = value; }
Would it be that hard for ColdFusion to do the same? I'm tired of writing getter and setter functions (and writing and maintaining tools that do it for me). Other automagic solutions, such as onMissingMethod and mixins, result in what I call a "squishy" API: you don't know its form until runtime. Hard to document!
Here's something I'd love to see in ColdFusion 9:
...works exactly like now. It declares a property, but doesn't set it, just like "public var firstname:String". Developers work directly with instance.firstname.
When we need to "hide" data and have getter/setter logic applied, we can expand our property:
The <cfsetter> tag is basically a <cffunction> tag invoked when a developer performs an assignment to firstname, receiving a single argument named "value." When the firstname property is accessed, the <cfgetter> function is invoked.
Gareth wrote on 11/26/07 9:26 AM
Seems logical to me. Definitely much easier in Flex for adding getters and setters. If I don't use some kind of auto-code-generation to create the CF getters and setters, the task gets quite monotonous.