I have made a few posts in the past using CFInvoke to call a method to do some work for me then return a result. With CFInvoke you need to pass an argument collection or use CFInvokeArgument to pass in arguments to the CFC method. This code can get tedious and convoluted if you have a lot of arguments.

For the sake of  at least brevity but sometimes performance, ColdFusion now is giving you direct access to the JRE it is built on by allowing you to call functions directly. One neat function you can use is CreateObject. I was surprised to find out that many developers i have encountered did not even know it exists, so here it is.

CreateObject is the same as CFInvoke except that it is a lot quicker to write. It is shorter as far as length of code, and I believe performance wise it is faster because CF does not have to do anything to the code like CFInvoke. It is already written for the JRE so it can do its magic. How short is it? I will show you.


        obj = CreateObject("component","dot.path.to.cfc");
        objMethod = obj.method(argument1=value,argument2=value);

So as you can see that it makes your life easier and quicker to invoke a component as opposed to the original way.


     <cfobject name="nameofobject" component="dot.path.to.cfc">

        <!--- you can pass the arg inline but i think the best practice is to pass arguments via cfinvokeargument----->

     <cfinvoke component="#nameofobject#" method="nameofmethod" returnVariable="returnVar">
         <cfinvokeargument name="argument1" value="test">
         <cfinvokeargument name="argument2" value="test">
         <cfinvokeargument name="argument3" value="test">

As you can see option 2 can be a lot longer and messier. and personally i think slower because ColdFusion needs to process the tag and convert it to a form that the JRE will understand. I would like to open a conversation to this point and find out if there is experience out there to let me know if this theory is true. So here it is … enjoy, happy coding!