NO MORE CAREER
POLITICIANS!
Get Out Of Our House: Replacing congress with TRUE citizens!
Contact Doug!
Learn About Doug!
View Doug Boude's online resume
updated 11/18/2009

View Doug Boude's profile on LinkedIn
Link to me!

Follow Doug Boude on Twitter
Follow me!

Be Doug's friend on Facebook
Befriend me!
(I promise not to follow you home)
OO Lexicon
Chat with Doug!
Recent Entries
You may also be interested in...
Web Hosting

best web hosting - top web hosting sites, thetop10bestwebhosting.com

Czech your Page Rank!
Check Page Rank of any web site pages instantly:
This free page rank checking tool is powered by Page Rank Checker service
Surf's Up!
Visit Egosurf.org and massage YOUR web ego!
My Score: 9,001
Doug's Books

Read (and recommend)

  • Men are from Mars, Women are from Venus
  • The Wisdom of Crowds: Why the Many Are Smarter Than the Few and How Collective Wisdom Shapes Business, Economies, Societies and Nations
  • Blink: The Power of Thinking Without Thinking
  • Head First Design Patterns
  • Transact-SQL Programming
  • What's So Amazing About Grace?
  • Just So Stories (Rudyard Kipling collection)

Reading

  • Prayer: Does it Make Any Difference?
  • Data Mining (Practical Machine Learning Tools and Techniques)
<< July, 2007 >>
SMTWTFS
1234567
891011121314
15161718192021
22232425262728
293031
Search Blog

Recent Comments
Re: Railo 3.1 on Windows Server 2008 and IIS7 - Part 3 of 3 (by Jon at 8/27 2:04 PM)
Re: Hosts File Changes Not Acknowledged on Vista 64 (by Spacy at 8/24 3:46 PM)
Re: THE DAY CFUNITED DIED (by ComboFusion at 8/23 10:50 AM)
Re: My Grandpa (by Tasha at 8/10 4:29 PM)
Re: Just What IS a 'Service Layer', Anyway? (by dougboude at 8/02 10:10 AM)
Re: Just What IS a 'Service Layer', Anyway? (by Isaac at 8/02 2:25 AM)
Re: PayPal IPN Coldfusion CFC (by Soyestudiambre at 7/25 6:12 PM)
Re: PHP vs COLDFUSION (by Tony Garcia at 7/17 11:24 AM)
Re: PHP vs COLDFUSION (by dougboude at 7/14 8:45 AM)
Re: PHP vs COLDFUSION (by Lola LB at 7/14 5:51 AM)
Categories
Archives
Photo Albums
Funnies (5)
Family (3)
RSS

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!!!

5 plus 16 equals
Type in the answer to the question you see above:

Your comment:

Sorry, no HTML allowed!