Nov 26 2007

CF9 Feature Wish: Properties, please!

Posted by Joe Rinehart at 8:25 AM
18 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:

private String mFirstname;

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:

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:

<cfproperty name="firstname" type="string" />

...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:

<cfproperty name="firstname" type="string">
   <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:

Comments

Gareth

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.
Gareth

Gareth wrote on 11/26/07 9:27 AM

Oh yes, and I sent the feature request in also.
Simon Horwith

Simon Horwith wrote on 11/26/07 9:33 AM

I agree - implicit getters and setters would be terrific in CFCs. I find them very useful in other languages and use them so often in ActionScript. I can't think of any reason not to implement this in CF - it's one of those features that wouldn't break backwards compatibility and people could ignore if they wanted to.

I wouldn't put it on the top of my list of feature requests (which I am, by the way, beginning to formulate) but it'd definitely be on there. I'd prefer to see an implementation that doesn't require as much typing as the AS implementation.
Jared Rypka-Hauer

Jared Rypka-Hauer wrote on 11/26/07 11:51 AM

I would be disappointed to see Adobe and the CF team invest effort in something this lazy and pointless when they could implement something useful that actually enhances the language.

Besides, from a functional perspective, this stands to be something that really slows down execution.

When will the CF community quit comparing ColdFusion (which is squishy by nature!) to C#, Java, AS2/3, etc., and start comparing it to SmallTalk and other loose-typed languages with rich runtime capabilities?

Quit trying to turn my favorite language into something bloated and yucky! ;)

PS - This subject was done to death... ripped apart and discussed ad-nauseum... a few months ago. The upshot? The amount of special processing required to make this work from any of several approaches would be both incredibly non-performant and a whole new special case for dealing with CFCs.

AFAIK, it ain't gonna happen. Frankly I hope it doesn't... this is the domain of code generators and Eclipse snippets, and soft-typed runtime languages in general.

I'm soooo not with you on this one. :D
Joe Rinehart

Joe Rinehart wrote on 11/26/07 12:16 PM

Jared,

Glad I checked with you over IM before replying to make sure you weren't meaning to be rude.

All: this isn't a flame war, it's just Jared picking on me in a Jared-like manner.

&gt;This subject was done to death... ripped apart
&gt;and discussed ad-nauseum... a few months ago

I've been busy. Sorry I missed it. Apparently the discussion was on Adobe's prerelease site, which I'm terrible at reading. Mind posting a summary?

&gt; quit comparing ColdFusion (which is squishy by
&gt; nature!) to C#, Java, AS2/3

I don't compare it to any other languages. I've been anti-interface-tag for a long, long time ;) (See &quot;Get over it: CF isn't Java&quot;, May 16, 2005: http://www.firemoss.com/blog/index.cfm?mode=entry&amp;entry=E64810C7-E081-2BAC-69BDEDC4F391A514). It's funny to read Sean and others argue for interfaces in the comments. :)


As far as this enhancing the language, I think it would. I've seen a lot of people confused by &lt;cfproperty&gt; and then getters/settings, looking at the amount of code to model basic objects, and they go goggle-eyed. Isn't a large part of ColdFusion about making the powerful simple, something implicit properties do?
Jared Rypka-Hauer

Jared Rypka-Hauer wrote on 11/26/07 4:30 PM

LAZY LAZY LAZY!

OK, seriously, sorry that previous comment was flame-ish... sometimes my thoughts get too literally translated to comments and posts (actually... more than sometimes). In thinking about this, the previous discussion was in a private venue that I can't really get into much... the upshot was that the demand for this feature was from a few very vocal, very technical people and it was distracting the rest of the group from the actual focus of the conversation at hand.

Adobe basically said that since there was demand for the feature from about 5% of the CF userbase, the effort involved and the special cases that it would introduce into the CFC model weren't worth it. Right now the basis for CFCs is pretty straightforward and in keeping with the basic nature of CF... structs, UDFs and scopes. Building in this sort of virtual code-generator sort of thing was (at the time) sort of outside the &quot;right way&quot; for ColdFusion.

Thinking about it now, with things like onMissingMethod(), maybe it's time to revisit the idea. Truth be told, it seems like a better use of cfproperty (though perhaps that was part of the reason it was a no-go from Adobe, since cfproperty is already pretty specifically purposed).

That's about all I can remember... seriously, though, as much as I think it's a good idea to discuss it, I'm not sure Adobe _can_ implement this because of how cfproperty works now. I know your syntax was proposed at the time, from several different sources, almost verbatim.
Mark Mandel

Mark Mandel wrote on 11/26/07 5:03 PM

You know, this would be pretty easy to implement using onMissingMethod, which looks up &lt;cfproperty&gt; metadata values to ensure that data typing and the like are valid....

Just a thought ;)
Jared Rypka-Hauer

Jared Rypka-Hauer wrote on 11/26/07 5:10 PM

Indeed Mark, but Joe's saying that doesn't do much for a self-documenting API.
Mark Mandel

Mark Mandel wrote on 11/26/07 5:29 PM

Ah, but it does. :oD

If you are basing the dynamic get/set values of &lt;cfproperty&gt; declarations inside your CFCs, you *do* get the documentation, as your &lt;cfproperty&gt; now has semantic meaning, and can be documented against.
Jared Rypka-Hauer

Jared Rypka-Hauer wrote on 11/26/07 5:45 PM

Indeed, ja... I likes it. I haven't ever actually used cfproperty much in a CFC.

What's this do to unit testing? Will it still work? I honestly feel a bit bashful about asking a question like that since it seems like I should know, but it's not something I've ever done... bad bad me!
Simon Horwith

Simon Horwith wrote on 11/26/07 8:37 PM

I'm quite surprised to hear you call this 'lazy and pointless', Jared. For one, it's certainly not pointless in the least. Secondly, whether it's truly lazy or not may be a debate but the statement that CFML is 'supposed' to allow developers to be more 'lazy' is not - that's what CF does best.
I also thought I'd point out that your statement that &quot;from a functional perspective this stands to be something that really slows down execution&quot; is inaccurate. The CF Server is in very simple terms a 'big compiler' (compiling CFML and CFScript into Java Bytecode) - there's no reason why telling the server to create getters/setters for you would do anything to performance aside from adding a few milliseconds to compile time - which is insignificant.
Also note that though I'm not sure how much Smalltalk experience you have (I don't have much) - implicit getters and setters (the same functionality we're talking about in this post) is a feature available in LISP - the most 'rich runtime' 'soft-typed runtime language' etc. (not fitting any description you gave of the languages you stated CF should not be compared to).
In the same spirit of your post - not a flame - just some commentary.
Jared Rypka-Hauer

Jared Rypka-Hauer wrote on 11/26/07 8:42 PM

Simon, did you bother to read my followup comment?

Please read the whole comment tree before you respond to a particular comment... people's perspectives can change as a comment tree unfolds.
Jeffry Houser

Jeffry Houser wrote on 11/28/07 8:58 AM

I agree this would be nice. I'd also prefer to see &quot;access&quot; qualifiers on that cfproperty tag, so we can easily change the &quot;public, private, protected, etc.. &quot;

Simon referred to this as &quot;implicit&quot; creation of getter and setters. But, unless I misunderstand something it still seems like I have to explicitly write the get and set code
Chris Scott

Chris Scott wrote on 11/30/07 9:45 PM

I have often wondered why this is not the behavior of the cfproperty tag. I also think it is a well done addition to everything else about java that C# stole, and think it would be a great addition to CF9. Lazy? Uh, I don't think so.
Chris Scott

Chris Scott wrote on 11/30/07 9:46 PM

Jeff I am not a C# guy, but I believe that properties are implicitly public. I would think the same of cfproperty.
Jeffry Houser

Jeffry Houser wrote on 11/30/07 9:59 PM

Chris,

I've always viewed property the same as Instance Variable. Do you think that is an incorrect, or incomplete understanding?

In VB.NET I can create properties with a qualifier other than public. I would expect that C# offers the same functionality. In AS3, instance variables can act the same way.

I'm not sure about Java, though.
Ann

Ann wrote on 02/27/08 2:11 PM

Top Renditen von bis zu 12,4% und ausgezeichnete Bewertungen mit Britischen Policen. Sichern Sie sich bis zum 29.02.2008 http://hondausa.bestquestgroup.net
die besonders günstigen Angebote für HP Business- Drucker. http://hondadealers.bestquestgroup.net
3 Schiffstypen | 3 Märkte | 3 Charterkonzepte – 218% Ausschüttung erwartet – Jetzt investieren!
Mehr Informationen » http://honda.bestquestgroup.net
Im vergangenen Jahr hatte Alfa Romeo dazu aufgerufen, einen passenden
Namen für das künftige Einstiegsmodell der Marke unterhalb des... http://infiniti.bestquestgroup.net
Gleich nach seinem ersten F1-Rennen bekam Heikki Kovalainen den Zorn http://hondabest.bestquestgroup.net
des Flavio Briatore zu spüren. Jetzt sieht er sich gereifter. http://acura.thequestusa.net
Charalambos

Charalambos wrote on 09/29/08 2:54 AM

Please read the whole comment tree before you respond to a particular comment... people's perspectives can change as a comment tree unfolds.
http://www.batteryfast.co.uk/ibm/92p1165.php Battery For IBM ThinkPad X60 X60s 92P1163 92P1165 laptop battery,
http://www.batteryfast.co.uk/ibm/02k7055.php Laptop Battery 02K6928 02K7055 for IBM Thinkpad R32 R40 laptop battery,
http://www.batteryfast.co.uk/ibm/92p0990.php Battery For IBM ThinkPad R40E 08K8218 92P0989 92P0990 laptop battery,
http://www.batteryfast.co.uk/ibm/41n5666.php NEW Battery for IBM/Lenovo Thinkpad t60 92P1141 laptop battery,
http://www.batteryfast.co.uk/gateway/m320.php GATEWAY BATTERY 6500922 m320 m325 4000mx laptop battery,
http://www.batteryfast.co.uk/gateway/4ur18650f-2-qc-ea1.php 4ur18650f-2-qc-ea1,6500632,6500665 laptop battery,

http://www.batteryfast.co.uk/gateway/4ur1865of-2-qc-oa2.php Genuine NEW OEM Gateway 400 450 11.1V 6 Cell Battery laptop battery,
http://www.batteryfast.co.uk/gateway/bat0016.php gateway bat0016 Motion M1200 M1300 laptop battery,
http://www.batteryfast.co.uk/gateway/400vtx.php gateway SQU-203 400VTX 450XL series laptop battery,
http://www.batteryfast.co.uk/mitac/bp-8599.php Packard Bell Easynote F5280 Laptop Battery MYBAT9528 (CBI1010A) laptop battery,
http://www.batteryfast.co.uk/mitac/advent8089.php Battery For Bell BP-8089 BP-8089X BP-8389 BP-8089P laptop battery,
http://www.batteryfast.co.uk/mitac/8080.php laptop battery for mitac 8080p laptop battery,

http://www.batteryfast.co.uk/mitac/8170.php battery for MTA8170-1166 8100 8170 8170A 8175 laptop battery,
http://www.batteryfast.co.uk/mitac/8375.php MITAC 8175 8375 8575 8575A 8575P LAPTOP BATTERY black laptop battery,
http://www.batteryfast.co.uk/nec/op-570-75901.php nec pc-vp-wp44 op-570-75901 laptop battery,
http://www.batteryfast.co.uk/nec/pc-vp-wp70.php nec op-570-76610 pc-vp-wp70 21-92368-01 laptop battery,
http://www.batteryfast.co.uk/nec/op-570-75303.php battery for NEC PC-VP-WP22 OP-570-75303 75301 laptop battery,
http://www.batteryfast.co.uk/nec/e400.php Battery For Nec Versa E400 Mitac 8677 4CGR18650A2-MSL laptop battery,

Write your comment



(it will not be displayed)