Object Orientation in ColdFusion is Probably Simpler Than You Might Think

I have a lot of compassion for anyone learning on their own, confronting CFCs, object orientation, the slew of frameworks that use CFCs in an object oriented manner all at the same time. It's overwhelming, especially for someone with no formal education in programming.

When CFMX 6 came out and CFCs were first introduced, i bought a book by Hal Helms called Discovering CFCs. He introduces some basic OO principles in there, all of which are pretty easy to understand in theory. Then he starts off showing examples. I still remember how disconcerting it was to not be able to understand. And i couldn't put my finger on what it was that i didn't understand.

Looking back, i think there were a lot of little pieces i just didn't get.

Today, if i glance through the book, it all looks very simple and elementary. But i needed to do a lot of struggling to get to that point.

Seeing that i didn't understand something, and not knowing really what it was, i asked some questions and got some very good advice which books on OO patterns to read. So i bought a bunch of those and started reading about patterns.

That didn't help much. Keep in mind that i'm doing this on my own, in my spare time. So months have gone by and i'm not getting anywhere.

Finally i decided the only way i'm going to learn this is to try it out ... to build something. But i had a big problem in that at that time, there were no OO frameworks to piggy back on. I had to architect the whole thing myself. And i didn't know where to start, at all. I tried using UML, i had my nose in and out of my OO patterns books, trying to figure out if any of them fit.

That didn't go anywhere.

So i went back to some basic OO concepts and just wrote down all the nouns in my application on a piece of paper. And out of desperation more than anything else for having spent months going around in circles without getting anywhere, started trying to get something to work with one object for each noun.

Along the way, i found out that i didn't know a lot of little things. I didn't really understand how inheritance worked when i tried to actually use it with my CFCs. I didn't understand how composition worked either really. I was confronted time and again with situations where my Nouns as CFC's needed to work together somehow, but they were all stuck on their own islands, enclosed in their little encapsulated variable boundaries.

I remember the moment i began to think i understood what the word instantiation meant. Or the moment i began to get what it was to pass something - a variable or a structure or even another CFC, into a CFC. ("Oh, you can do that??!")

In a way, all i could do was hang in there like Helen Keller at the OO water fountain, and wait for some light bulb to click in my mind, not knowing which one it might be at any given time, or even if there is one.

It really doesn't have to be so difficult to learn to use a little OO in CF. Perhaps a few blog posts covering those little things that i didn't understand might help someone else? Look for them in the coming days ... or weeks.

Related Blog Entries

Comments
Hey Nando,


I had a lot of "Oh, you can do that?!?!" moments myself. Learning OO is a process and can even be rather frustrating. The biggest tip I was given is to try out various things in a scratch pad area. If I had a question how something worked, I played around with it in the scratch pad area until I got it sorted.


I've certainly learned a lot from your blog posts and many posts on mailing lists. Thank you for your contribution to the community.

Dan Wilson
# Posted By Dan Wilson | 3/14/07 6:08 AM
I was in the same boat, sort of. I have no formal training but I too, pushed myself. I also learned PHP and JS and they used functions so that made it easier to grock CFC's. But I still struggled with php classes... I just didn't get it. Then I took on a project and used cfc's to do all the database calls and that helped a great deal. Then I looked at the PHP classes for the WIKI I use and all the sudden light bulb went off...

I also fund that my FuseBox experience really helped in getting my head around Mach II. After that it all kinda clicked.
# Posted By Cozmo | 3/14/07 6:28 AM
Oh, yes, please do! In clear, simple English . . .
# Posted By Lola LB | 3/14/07 6:56 AM
I'd appreciate some of those 'little things learned along the way' posts.
# Posted By James, F.E. | 3/14/07 7:49 AM
Totally agree on the "lightbulb" comments! I started learning OO in 1992, on my own, when there were next to no introductory books (and my learning language was C++ making it even harder). The idea of pulling all the nouns out of your application is great - definitely a good way to start a design. The next step is to think about how they need to collaborate, i.e., what methods they need in order to work together to get things done. Then, where you have dependencies (calls from one object to another), think about whether one object should "own" a reference to another object or whether it should ask some intermediary (and "own" a reference to that instead) or whether the intermediary should help the two objects collaborate (this is absolutely the hardest part to think thru and there are no black and white rules - you might even revisit this several times as your application grows).

Couple of points here:

1. Hal's book was written for CFMX 6.0 and is extremely out of date now (it talks about THIS scope everywhere because, back then, there was no working VARIABLES scope, no VAR scope and no thread safety). Just a caveat for anyone thinking about buying it.

2. You RSS feed is a bit broken - it doesn't encode spaces in URLs so quite a few RSS clients won't be able to jump to the actual articles.
# Posted By Sean Corfield | 3/14/07 8:35 AM
Hi Nando,

Nice post. To follow up on Sean's comments:
(a) I couldn't get to this article from Bloglines so you do want to look into the feed!
(b) On the design side, check out Ward Cunninghams CRC cards. For more complex apps it can be a reat way to really start to "get" OO.
# Posted By Peter Bell | 3/14/07 2:12 PM
I can totally relate to Nando's journey. I've had similar struggles, but for different reasons.

I learnt OO in about 1984, with smalltalk-80 and Adele Goldberg's book. I did a bit with THINK Object Pascal, then a loong gap until CFMX finally arrived (which at my company wasn't until about a year ago).

I found OO concepts very natural and very simple. Start with Logo, turtle graphics etc. It *is* simple. Learning the ins and outs of the Smalltalk image - not so simple, but always a clear path forward.

I started with CF in version 4.0. No function calls! In retrospect we were lucky to have skipped version 5, because I know I would have been coding function lookup tables to try to get some OO happening, and that would have all been wasted when - ta da! - CFMX came along.

Getting into CF OO was incredibly confusing. Classes aren't objects. No class methods. And what are all these beans/daos/gateways/services? So I started reading, and immediately ran into the enormous amount of baggage that CF carries, and which I think makes it a terrible environment for learning OO.

Firstly, J2EE casts a long shadow. Are you writing enterprise distributed transactional systems, or trying to get a handle on OO concepts? Both at once? You poor bastard.

Secondly, J2SE can be a pernicious influence as well. Why is Java statically typed? Basically to improve performance. So when looking to Java for inspiration, we're looking through the fog of a performance optimization that doesn't apply to CF anyway.

Thirdly, while I don't think anyone claims design patterns are going to help you learn OO, a lot of newbies seem to end up with the patterns books very early on. We need to make the message clearer - most of these patterns are complicated solutions for complicated problems.

Then there are CF's own performance issues. We pull our punches with OO modelling in CF, because the platform can't cope with instantiating lots of objects. Not a crippling problem, but not a good learning environment either.

Now, I don't realistically expect anyone is going to get Squeak or Logo or similar and spend time learning it as part of their CF skills update program, although I think that would be a good idea. But I do think a conscious effort needs to be made to peel away J2EE, Java, complex patterns, fear of object instantiation, worrying about the database layer, etc, etc.

That won't actually make OO in CF simpler, but it will get all the stuff that has nothing to do with OO out of our way so we can actually learn something.

Jaime
# Posted By Jaime Metcher | 3/14/07 4:04 PM
Following up on a few things here, Hal's Discovering CFC's book is unfortunately out of date, as Sean says. I wish it wasn't, because the community could really use a book like that. But i also would not recommend that anyone new to CFC's buy it at this point. He talks about problems with CFCs that don't exist anymore and suggests workarounds to them that will simply confuse the heck out of anyone trying to learn at this point. Again, unfortunate because in many respects the book is very well written and presented in a very clear manner.

Hopefully a few blog posts here on the basics of using CFC's in an object oriented manner can help fill the gap.

And i can only wholeheartly agree with Jamie's statement about pattern books for people new to OO: "We need to make the message clearer - most of these patterns are complicated solutions for complicated problems." If you're new to OO and coming into it from a background in ColdFusion, i can only suggest that you focus on the basics first. Otherwise it's a little like taking a course in French law at a Paris university without knowing how to order a coffee and croissant from the corner cafe yet! I've tried it, i know!

And i just cleared out the alias i tried to set for this post. (Aliases shouldn't have spaces in them apparently.) Hopefully that clears up the RSS problem!
# Posted By Nando | 3/14/07 7:53 PM
Anyone familiar with cars and upgrading the engine knows about how all the parts interrelate with each other. If you upgrade the intake without upgrading the carburetor (boy am I dating myself) or the exhaust you don't get the full benefits; they all have to be planned out to work together otherwise all you are doing is moving the bottleneck around. Well in OO my challenge is that I only know a few isolated concepts, not how it all works together. I have developed a system for how my pages all work together now and in order to convert to a true OO style, I have to know how I need to change how all the parts work together... it all has to work from end to end, a single concept by itself doesn't do any good. Some sort of transition from CFWACK programming to OO is in order but non-existant (unless you are going to a Hal Helms class!)
# Posted By Michael White | 3/15/07 7:12 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.1.004.