Doug's Resume
OO Lexicon
Chat with Doug!
Recent Entries
You may also be interested in...

heaters
hotels boeken in 7 sec
Engagement Rings
Online Dating Australia




SURF'S UP!
You:
Your Web Site:
<< May, 2008 >>
SMTWTFS
123
45678910
11121314151617
18192021222324
25262728293031
Search Blog

ColdFusion Jobs
Recent Comments
Re: Viewing Option Text (in IE7) that's Wider than the Select List (by Dan Roberts at 5/15 2:38 PM)
Re: Viewing Option Text (in IE7) that's Wider than the Select List (by Dan Roberts at 5/15 10:06 AM)
Re: Inline CSS and Ajax Issue with IE (by Ben Nadel at 5/14 11:50 AM)
Re: The Perfect Alternative to Gas Powered Vehicles (by Thomas Messier at 5/09 12:47 PM)
Re: Promoting Family Unity: Lowering Your Utility Bills! (by Fernando Lopez at 5/07 10:12 PM)
Re: Why I Hate ORMs (a solicited rant) (by Richard at 5/06 10:56 AM)
Re: Why I Hate ORMs (a solicited rant) (by dougboude at 5/06 10:27 AM)
Re: Why I Hate ORMs (a solicited rant) (by Richard at 5/06 6:50 AM)
Re: Why I Hate ORMs (a solicited rant) (by Sean Corfield at 5/06 1:40 AM)
Re: Why I Hate ORMs (a solicited rant) (by Steve Bryant at 5/05 5:07 PM)
Categories
Archives
Photo Albums
Funnies (5)
Family (3)
RSS
Reciprocal Links

Powered by
BlogCFM v1.11

08 October 2007
Appropriate Usage of the "THIS" Scope
For the past several days, my good friend Jim and I have been having a series of discussions regarding the use of the 'THIS' scope within CFCs; specifically, CFCs that are to be used in an OO fashion. He's part of a small team that has just begun their OO journey and are embarking on a project where they hope to grow and refine their understanding in this arena. Jim being the stickler for correctness that he is, he often bounces approaches and ideas off of me to see what my take is, as well as compare the code he produces to that of his fellow teammates. Here is where a certain rift has manifested itself among them, as his fellow coders are producing CFCs that make heavy use of the 'THIS' scope and little to no use of 'Variables' and 'Var', while Jim is not using 'THIS' at all and is rather declaring his variables as public within the 'Variables' scope or private using 'Var'.

My personal take on the use of 'THIS' within a CFC (and what I firmly expressed to Jim) is that it would be a rare circumstance when I would see 'THIS' as the appropriate scope, since putting variables into 'THIS' exposes them to actions performed outside of the CFC. Just visualizing a CFC with all it's internal variables in the 'THIS' scope immediately causes me to think, "You've just reduced your object down to a very primitive state...a Transfer Object. Why don't you just rather create a structure to hold those values since you're ACCESSING them as if your object WERE a structure." Basically, I've been telling Jim that using 'THIS', except in very specific circumstances, is a bad thing to do.

His own intuition and experience up to this point tells him the same thing, since his comprehension of 'Variables' and 'Var' is quite clear. He has as of yet, however, been unable to convince his peers of this, and has even been admonished to STOP using 'Variables' and 'Var' and start putting his CFC variables in 'THIS'.

Because Jim is a stickler for learning to do things the "right" way, he set out on a Google journey to find documented proof that what he and I believe regarding 'THIS' is absolutely correct. His search has left him empty-handed, though, and so he and I turn to you, our fellow developers, for input on this topic.

The question then: What are your basic rules of thumb regarding the use of 'THIS' when writing a CFC to be used in an OO application (even a loosely structured one)?

Thanks in advance for sharing your opinion!

Doug  :0)



Posted by dougboude at 11:45 AM | PRINT THIS POST! |Link | 6 comments
Subscription Options

You are not logged in, so your subscription status for this entry is unknown. You can login or register here.

Re: Appropriate Usage of the "THIS" Scope
I am not even close to being an OO expert, but this is my gut feeling on it.

If I use the variables scope and build get/set methods to access them, I've created an abstraction layer around my object. This is nice because I may need logic in setAge that checks for numeric and a value greater then 0.

However - I almost always use 'dumb' get/sets, so the above reason is BS. ;)

Even with that - I feel like having an API (get/set) to work with my CFC makes my code more portable and simpler to update later on. THe only thing I don't like about it all the grunt work. BUt - CF8 makes that go away as you can build beans and use onMissingMethod to handle all the grunt work.
Posted by Raymond Camden on October 8, 2007 at 12:43 PM

Re: Appropriate Usage of the "THIS" Scope
Doug,

what better way to comment on your post but by using a simple example. I've put one together here http://blog.tech-cats.com/2007/10/coldfusion-cfc-scope-exploration.html. This should clear up any confusion on the subject. Enjoy!
Posted by Boyan on October 8, 2007 at 12:55 PM

Re: Appropriate Usage of the "THIS" Scope
I think the use of THIS is very much dependent on convention. Why go through the hassle of writing one-line getter/setter functions (actually, this sums up to at least 7 lines of code) for each property that you don't mind to be public?

Looking at it from a user (class consumer) perspective - where is the benefit of wrecking havoc with the instance properties when you actually want the object to do something useful?

Given that the crown jewels of the class are properly hidden away, using the THIS scope cannot hurt so much. It is sad, though, that in CF it is not possible to use a JavaBeans getFoo/setFoo syntax that can later be referenced as someObject.Foo - this would give the opportunity to start in the THIS scope and write getter/setter methods as they become necessary - without having to touch every page that uses the class.
Posted by Martin Böhm on October 8, 2007 at 1:42 PM

Re: Appropriate Usage of the "THIS" Scope
"This" refers to the object (CFC) you've instantiated and is in the variables scope of a CFC itself.

http://www.iknowkungfoo.com/blog/index.cfm/2007/8/22/Object-Oriented-Coldfusion--2--Anatomy-of-an-Objectcfc

The only time I've ever used "this" is when I've instantiated an object that extends another object and I needed to specify a call to a function from the child object ( this.foo() ) vs. a function of the same name in the parent object ( super.foo() ).
Posted by Adrian J. Moreno on October 8, 2007 at 1:42 PM

Re: Appropriate Usage of the "THIS" Scope
Thanks for all your input on the subject. My friend Jim (whose Google Odyssey never ends) pointed me to this really great discussion thread on the subject over at Brian Kotek's blog ( http://www.briankotek.com/blog/index.cfm/2007/2/6/VarScoping-Private-and-Public-Data-in-CFCs ). After reading through the entire comment thread, my conclusion is that when a developer wishes to adhere as closely as possible to OO best practices, the THIS scope is used sparingly and always with good reason and purpose.
Posted by dougboude on October 8, 2007 at 4:31 PM

Re: Appropriate Usage of the "THIS" Scope
I use THIS scope in only two situations:
- a value CFC result returned from a remote method
- for "constants" associated with a CFC

The second one is easier to explain but really is not a very good use for it (I must stop doing it). I have a service, fooService, and it has some "magic" values associated with its API. I give them symbolic names using THIS scope variables:

cfcomponent
this._SOME_CONSTANT = 1;
this._ANOTHER_CONSTANT = 37;
...

That makes them easier to access and easier to spot in client code but, to be honest, it's a somewhat lame practice that harks back to my C++ / Java roots and it's not really a good idea. Old habits die hard.

The first scenario is just per the CF docs for web services or Flex remoting where you have cfproperty tags to declare publicly accessible data and then use THIS scope for the actual data members rather than variables scope and getters. The CFCs are pure value objects, no methods at all. They'd be structs if they weren't providing named type information in the return type of the web service / remoting method.

See CF8 LiveDocs for an example:

http://livedocs.adobe.com/coldfusion/8/htmldocs/webservices_20.html

Other than that, I never ever use THIS scope.

But then I also have implicit get/set methods for my CF8 projects due to the onMissingMethod() in my (modified) base component.cfc - see the article in FAQU 5 (CF8 edition) when it comes out in the next few months.
Posted by Sean Corfield on October 10, 2007 at 1:23 PM

Name:   Required
Email:   Required your email address will not be publicly displayed.

Want to receive notifications when new comments are added? Login/Register for an account.

Time to take the Turing Test!!!

What letter comes two place(s) before the letter I?
Type your answer exactly four time(s) in the designated box.

Type in the answer to the question you see above:

Your comment:

Sorry, no HTML allowed!