ColdFusion and OOP - Match Made in Heaven, or Long Road to Hell?

Hal Helms, Ben Nadel and I recorded a conversation over the weekend on the subject of OO in CF. I'm supporting the position that OO is still a good thing in CF, Hal disagrees, and Ben is undecided. As you might expect, Hal is left a trembling husk as the weight of my arguments unmercifully crushes him. You know those scenes in superhero movies where someone gets punched so hard they end up in a crater in the ground? It's like that. Only worse.

In all seriousness though, it was a very fun talk and I think there are some solid points made from all involved, but I'll let you, gentle reader, be the judge. You can download the recording here.

In the interest of keeping any discussion on this topic from fragmenting, we've decided to disable comments on our respective blog entries and instead created a Google Group to act as a central sounding board. Hopefully this isn't too inconvenient, I realize it's something of a departure from the norm, but let's see how that works.

I'm not sure yet whether this will turn into any kind of regular discussion, but I suppose it could. We'll just have to see what folks think! Thanks.

del.ico.us del.icio.us | Digg It! Digg It! | Linking Blogs Linking Blogs | 2115 Views

OO Can't Ruin Businesses, but People Can

Blogging newcomer Marc Funaro made a provocative first post over the weekend with his entry How OO Almost Destroyed My Business. It has gotten a lot of comments, some supporting him, and some taking issue with his conclusions. I started to comment but decided it would be better to generate a secondary discussion rather than add onto the already long thread.

Marc says he picked up ColdFusion as a non-programmer, and had good success with it until fairly recently. With the movement toward object-oriented development that is happening in the ColdFusion world, he ran into trouble. He read some books, some blogs, and took a class on Java development. And he ended up overwhelming himself with unnecessary complexity in terms of frameworks, design patterns, and OO architecture. He sums up the result of doing this pretty nicely:

"The bottom line is, when you NEED to use some OO concept, YOU'LL KNOW. *That's* the time to start writing OO-style code, and only then... not everywhere else."

Which is pretty good advice. It's something that any knowledgeable proponent of OO will tell you. I'm sorry that it took him a good amount of time and frustration to reach that conclusion, but I'm glad he finally did.

Where he goes wrong, though, starts right in the title of his entry. OO can't ruin anything, but people making bad decisions absolutely can. And what Marc did was make some bad decisions, because he was new to OO, confused, and, as he says, "downloading one framework after another, piling them all into an application". This is like reading a book on construction, and then going out and trying to build the Taj Mahal when all that was needed was a garage.

Bad decisions don't mean a person is stupid or foolish. Smart people make bad decisions all the time (I'm not conceited enough to call myself a smart person, but I definitely have made some bad decisions). Usually, it's simply a lack of knowledge or experience, or a failure to understand the implications of the choices you're making. But even that is OK, because when someone makes a bad decision, it can still have a positive outcome if it results in learning something. OO does not equate to using a framework, and it does not require the application of every design pattern under the sun. It's simply a way to organize code, manage complexity, and accommodate change. Sometimes, that is best served by using a framework like ColdSpring or Spring. Sometimes, design patterns can offer solutions to encapsulate variations in a system and cope with change. One of the key things anyone using OO must understand is that there are pros and cons to every decision, and multiple solutions to a given problem. The only way to learn how to assess these trade offs is through experience.

The reality is that a lot of ColdFusion applications don't require a massive OO system to power them. Many of the small- or medium-sized applications don't need an n-tier architecture loaded with abstractions and design patterns. But that doesn't mean that some of the good ideas of OO, like encapsulation, can't be used with big benefits. One doesn't need to turn every query into an array of objects. Just creating well-defined interfaces to expose behavior to the rest of an application will get you a long way. Once something is encapsulated, it's much easier to change it later if you need to. It might be just a few CFCs to wrap up the bulk of the logic and hide the implementation. That might be all that will ever be needed. But if (and, more likely, when) things get more complex and it comes time to start adopting a broader set of OO principles, you'll be in a much better position to do so.

However, there is another reality that can't be denied: in the debate between procedural and OO development, OO has won. It won many years ago. ColdFusion is one of the few languages left that supports procedural development to a large degree. If you want to keep being a software developer, or ever want to move to a language like ActionScript, C#, Java, Groovy, or Ruby, you're going to have to know OO. That's just how it is. And as Marc points out, even within the CF world, OO is taking over, and the number of jobs available to people without OO experience are going to keep getting smaller and smaller. Some folks may not like this and may attempt to rebel against the trend, but you can't stop the tide. OO is not going away, in fact, it's only going to get ever more ubiquitous. So it's probably in your best interest to learn about it. One doesn't have to use it on every project, nor does one have to use it to create a complex, over-engineered mess. But experience is the best teacher, both in terms of learning OO and increasing your demand in the marketplace.

So, with respect to Marc, don't do what he did. Don't try to swallow the entire OO buffet in one bite. If you try, you'll fail. You'll get frustrated. And in that red haze, you'll probably miss the simple benefits of OO. Instead, learn what you can and take time to digest the information. Experiment with it, but don't get carried away. Apply what makes sense to you where you can, in small bits. Remember that the goal is to learn, but it is also to help you do what works for you and build applications that satisfy customers.

I suppose the bottom line is: Don't be afraid of OO. Be afraid of anyone who says that OO is the only way to build an application, and be just as afraid of anyone who blasts OO because they got carried away with it and got burned.

Comments Comments (23) | del.ico.us del.icio.us | Digg It! Digg It! | Linking Blogs Linking Blogs | 3012 Views

Groovy: The Invasion Begins

Past readers have probably seen some of my posts that include references to the Groovy programming language. At Broadchoice we chose Groovy for the entire server-side object model for our Workspace AIR application. In other words, I've been using Groovy for a while now. And I've been, and continue to be, extremely impressed with it.

If you're a ColdFusion developer, you may want to give Groovy a test drive. Because Groovy is a dynamic language, it plays almost perfectly with CF. You can kiss all those JavaCast() function calls goodbye when your CFML interacts with Groovy objects. Since Groovy runs in the JVM and compiles to bytecode just like CFML (and straight Java for that matter), you can reap its benefits at very little cost in terms of setup.

Further, to those with any familiarity with Java at all, the learning curve with Groovy is really short. Another of the many really cool things about Groovy is that it gives you a very wide spectrum of coding syntax:

On one side, almost any native Java code can be pasted into a Groovy class and it will run fine with no modifications. So any examples, libraries, or legacy code can literally be copied, pasted, and forgotten about, if you want.

But on the other side, Groovy adds a ton of convenience features on top of the core Java language. It takes things that require long, boring, boilerplate Java and incorporates it into the language in a way that just works. And you're free to take advantage of these great time-saving capabilities anywhere you choose to. That's what's so cool: you get to choose! Want or need "normal" Java. Fine. Want tight, terse Groovy? Fine. Want any combination of those? Fine. That's just really damn sweet.

Yesterday, one of the resident mad geniuses of the ColdFusion community, Barney Boisvert, released version 1.0 of his CFGroovy library that makes integrating Groovy (and Hibernate) into CF applications a snap. He also has a very nice post on why you should care about Groovy.

Because it is so seamless to use with CF, so powerful, and so easy to use, I really think we're going to start seeing a lot about Groovy. Add to that the fact that Groovy's object creation speeds, while slower than Java, are far beyond CFCs, and we have a candidate for really robust, large-scale object models while still being able to leverage CF anywhere else in the controller, model, or view that makes sense. So if you like to tinker, learn something new, or do large-scale OO, I'd urge you to check out Groovy and Barney's CFGroovy library.

Comments Comments (10) | del.ico.us del.icio.us | Digg It! Digg It! | Linking Blogs Linking Blogs | 3373 Views

Results of the ColdFusion OOP Survey

A couple of weeks ago I put up a survey to get some data on how current ColdFusion developers are (or aren't) using object-oriented programming (OOP) techniques. In all, 176 people took the survey, so thanks to everyone who took the time to go through it! As promised, I've done some number crunching on the results and am posting them for folks to see and comment on.

[More]

Comments Comments (10) | del.ico.us del.icio.us | Digg It! Digg It! | Linking Blogs Linking Blogs | 2829 Views

Taking the "Object Calisthenics" Challenge

In The ThoughtWorks Anthology there is a chapter called "Object Calisthenics". This section lays out a challenge to help push your understanding of object-oriented programming concepts. I decided to take this challenge by building a little Flex application, and I've been quite surprised by its effect. It really does make you think very deeply about what OOP means and how it affects the way you program.

In essence, the challenge is to write a non-trivial (about 1000 lines) program of your choosing that does not violate a set of 9 rules. The rules are very draconian, and they aren't advocating that you actually write all of your software this way (though obeying them in most cases probably would be a good thing). But the goal of the exercise is to really expose any lingering influence of procedural coding and force you to come to terms with them. While I chose to do this in Flex and ActionScript (since that is what I am currently learning in depth), I think some of these would probably need a bit of tweaking in a ColdFusion implementation, but most of it would still apply. Here are the rules:

  1. One level of indentation per method. If you need more than one level in from the start of the method body, create another method and call it. So one level of a loop or if statement is ok, but any deeper and you need to break it out into its own method.
  2. Don't use the ELSE keyword. This one is tough. We're very used to using if/else or switch/case. But good OO designs rely on polymorphism in place of conditional logic.
  3. Wrap all primitives and Strings. That means instead of var zipCode : String, you need var zipCode : ZipCode, and instead of var age : int, you need var age : Age. The idea is to ensure that everything is an object, that the purpose of everything is self-evident from it's type, and that behavior related to that object has somewhere to go.
  4. Use first class collections. This means you can't have var cartItems : ArrayList, but instead have var cartItems : CartItems. This means that behavior related to the collection has a place to live, and that the collection should contain no other instance variables.
  5. One dot per line. This is meant to enforce the Law of Demeter. So this would be a no-no: invoice.lineItems.getLineItem(4). Although this discourages method-chaining in cases where a method returns the same object (a la JQuery), that isn't what this rule is trying to do. It's trying to stop you from reaching across class boundaries and digging into the guts of other objects.
  6. Don't abbreviate anything. This is meant to enforce clarity, as well as identify duplication or misplaced responsibilities. If you're typing mergeUserPreferencesFromDatabaseAndCookies() too often, something is probably wrong, both in terms of what the method is doing and how many things are coupled to it.
  7. Keep entities small. No class over 50 lines, and no package over 10 files.
  8. No classes with more than two instance variables. This is meant to ruthlessly enforce the single responsibility principle for objects. If you need more instance variables, break them into composed objects.
  9. No getter, setter, and property calls to other objects. This mandates the principle to "Tell, don't ask" and enforces strong encapsulation boundaries.

I chose to write a program that generates a game of 10 pin bowling, and then scores it. And wow, writing code to score a game of bowling was actually a lot harder than I initially thought, even without the above rules. But I'm just about done with it and when I am I'll post it here in case anyone wants to take a look at it.

But before I post it, I thought I would ask if anyone else is interested in giving this a shot? Would anyone be up for trying this (with a bowling score card or any other idea you like), sending them to me, and having me post them (anonymously if people prefer) and start a blog discussion about them? I think it would be quite educational to see how different people approach these rules to solve problems, and I'm sure everyone involved would benefit. Any takers?

Comments Comments (14) | del.ico.us del.icio.us | Digg It! Digg It! | Linking Blogs Linking Blogs | 2520 Views

Previous Entries / More Entries