Sometimes, a query should just be a query

During a conversation today with another developer, I was reminded of a recent collection of blog posts by San Diego ADUG Manager, Robert Munn, that talks about this from a technical perspective, but I wanted to talk about it from more of a "best practices" slant.

I don't think this is quite as bad in 2008 as it was a couple of years ago, but there has been a propensity for CF developers who are trying to use MVC and create OO CF applications to lose sight of the KISS method of development - Keep it Simple, Stupid!

ColdFusion's beauty is in its simplicity, but there are many CF developers that seem to have a love/hate relationship with this simplicity. It's either they intentionally add a lot more complexity to their applications, or that they actually wish it were harder! We see CF developers that seem to trash ColdFusion every chance they get, and I can't understand why for the life of me.

I used to be a procedural developer, but I am definitely an OO developer and have been for a while now. I use Mach-ii and ColdSpring every day, and am pretty darn good at it! So, I don't want to sound like I am bashing OO. I am just saying that there is good, responsible OO, and then there's pseudo-OO, where there are getters and setters everywhere that really do nothing. Your objects need to DO something, and not just exist because they look like OO.

OO is great, but there's a time and a place for it. To be clear, I am talking pragmatism and using what the language is best at over theory. Not everything should have getters and setters and return an array of objects. I've known for some time now, anecdotally, that, most times, a query should just stay a query. There are no reasons to convert them into objects if they can be represented as a query. Unless they represent a single object that needs to be manipulated, you don't need them. Often, if you need a more complex structure, returning an array of structures, versus an array of objects, will give you MUCH better performance.

There is best practice, and maintaining good applications with no more code than you need. But, in Rob's blog, there's also legitimate technical reasons why you should use no more objects than you have to. Rob has a series of posts called "Object instantiation and heap size in ColdFusion".

Part I
Part II
Part III
Part IV

Rob is highlighting where objects are persisting in memory until the end of the request and how memory spikes up, where a procedural solution resulted in much faster processing and lower memory.

That's not to say that you shouldn't create arrays of objects, but Rob's was a great test to demonstrate that objects, a lot of them, are problematic in ColdFusion. Interestingly, keeping the object in memory actually HURT performance. As this demonstrates, it's not the instantiation of objects that causes the problems, but the bean persistence. Objects holding data tend to be memory hogs, even in CF8.

Here is probably the most significant statement made in the series of posts:

"It would seem that the use of components as beans to hold data creates a serious potential performance problem for CF-based apps in cases where those beans are persisted (e.g. in the application or session scope) or where many beans are instantiated in a single, long-running request. I don't see any problem using beans in other scenarios, e.g. using beans to store a small number of records returned from the database, or using a bean to model a form in an HTML or Flex front end. I would be cautious, though, of using a large number of beans in a request or persisting beans in the session scope where they may not be garbage collected."

In today's conversation, both of us agreed on this principle, but we haven't been working together all that long, so it was good to bring it up, and I thought I'd bring attention to the subject. Rob's blog posts on it hit home.

If you haven't found Rob's blog yet, I suggest you add it to your rotation.

TrackBacks
There are no trackbacks for this entry.

Trackback URL for this entry:
http://www.brianmeloche.com/blog/trackback.cfm?8A389081-3048-2E57-0A5D985090824420

Comments
Well, that's why you use an Iterating Business Object! You get the benefits of encapsulating your getter and setter login (and over time for non-trivial apps there WILL be such logic), but you're only instantiating a single bean for n-objects and it's as simple as IBO.loadQuery() to make it work.

I use it as my default practice and while there are times where I'll just use a CF Query, I find as a general purpose best practice this gives me the encapsulation benefits of OO with close to the performance benefits of a CF Query.

I'm going to be doing presos on this approach at CF United Europe, Scotch on the Rocks and Web Maniacs. And no doubt I'll be waxing lyrical about them over a beer at most of the other CF cons this year :->
# Posted By Peter Bell | 1/18/08 1:28 AM
# Posted By barry.b | 1/18/08 4:49 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.5.002.