Ask a Jedi: Two quick ColdFusion Component questions

This post is more than 2 years old.

Esmeralda (what a cool name) asks:

I am attempting to use Coldfusion Components. I have two questions - first, if there are multiple instances of a component on one page, would it be better to use cfinvoke or createObject? Secondly, is there an efficient way to use a component method within a loop? Currently it will include that file during each include, when all I really need is the method from said component. Shouldn't the component already be instantiated on the page and therefore not recalling that include?

Your first question is simple. Think of a component like a tool. You use the tool to perform some type of business logic and return a result. That being the case - there is no need to recreate the tool every time you use it, right? You don't buy a hammer for each nail. You can certainly use createObject to make the CFC at first, but after that, just use the instance you created.

Your second question is a bit weird to me. So yes, in a loop you can call some method on a CFC over and over again. That's not efficient or inefficient by itself. It sounds like you are cfincluding a page with the createObject, and if so, then yes, that is wrong and you should just make the CFC once before you get into the loop.

You can stop reading now as the following is mainly just theory. As a generic question, when does it make sense to do:

<cfloop something> <cfset cfc.foo(x)> </cfloop>

versus:

<cfset cfc.fooList(list)>

I don't know if I have hard and fast answers, but my thinking is that the a loop is certainly fine if you already have a loop. Maybe you are outputting over a query and part of the result comes from the CFC method. The first method is also simple: "Do some logic on a value", whereas the second, "Do some logic on a list of values" may not be as useful across multiple areas. I would probably guess that - over all - the speed may be better with fewer calls to the CFC method, since ColdFusion does have to 'package' up stuff and pass data, but I doubt the difference would be measurable even. I think I'd make the decision based on what makes the most sense for the API of your CFC and not worry so much about the speed difference.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Mat Evans posted on 8/4/2008 at 3:49 PM

Hi all,

Could you also make a loop method? So if you ever need to use a method on a list then you just send the list and the method you want to use to the loop method and it just loops for you, then you still have your original method and the loop method can be used for lots of different functions.

Just a thought.

Mat

Comment 2 by Joel posted on 8/4/2008 at 4:02 PM

About your first part of the question - you can use the methods repeatedly. As long as the methods don't affect the internal state of the object.

If the objects represent a physical object - calling the same cfc (by reference) could be problematic.

- Joel