4 Replies Latest reply on Jun 22, 2007 6:42 PM by Newsgroup_User

    To create an object or not?

    Level 7
      I was wondering what the better method of utilizing a component was. I know
      if you want to use multiple instances of a component it is better to create
      an object and use it to invoke various methods. However, if only using one
      method of a component, its it better to just invoke the component directly?

      <cfinvoke component="cfc.my.component" method="getStuff"
      returnvarible="myStuff"/>

      What is the benefits and drawbacks for invoking a method directly verses
      creating a cfobject.

      And while on the topic, which is better?

      <cfobject name="MyComponent" component="cfcs.my.component">
      or
      <cfscript>
      createObject("MyCompoent","cfcs.my.component")
      </cfscript>

      Again, what are the benefits and drawback of each. I am more familiar with
      the first using <cfobject> but I see the second more and more.
      --
      Wally Kolcz
      MyNextPet.org
      Founder / Developer
      586.871.4126


        • 1. Re: To create an object or not?
          craigkaminsky Level 3
          Wally,

          In general, I use cfscript to invoke my objects and their methods.
          <cfscript>
          objMyComponent = createObject("component","cfcs.myComponent");
          getStuff = objMyComponent.getStuff();
          </cfscript>

          Personally, I think the choice in your approach is a "6 in one hand, half dozen in the other" sort of thing. Both approaches work well. I did some tests some time ago and found that using cfscript invocation techniques tended to be slightly faster than tags. This is, admittedly, highly debatable and the performance differences I saw were negligible. Ultimately, I find cfscript a more comfortable way to code. It jives with other languages I use.

          For improving performance, I typically use Application.cfm or Application.cfc to invoke my commonly used objects, store them in a persistent scope for quicker access later. In Application.cfc, I often use something along the lines of:
          <cfcomponent>
          <cffunction name="onApplicationStart">
          <cfscript>
          application.objMyComponent = createObject("component","cfcs.myComponent");
          </cfscript>
          </cffunction>
          <cffunction name="onRequestStart">
          <cfscript>
          request.objMyComponent = application.objMyComponent;
          </cfscript>
          </cffunction>
          </cfcomponent>

          From there, I can just call the method in my desired template
          <cfscript>
          getStuff = request.objMyComponent.getStuff();
          </cfscript>

          Cheers,
          Craig

          P.S. Love your site's mission. My wife and I have two dogs we rescued and volunteer at our local shelter...love to see sites like this!!!
          • 2. Re: To create an object or not?
            Level 7
            > However, if only using one
            > method of a component, its it better to just invoke the component directly?
            >
            > <cfinvoke component="cfc.my.component" method="getStuff"
            > returnvarible="myStuff"/>

            Yes, I think this is better. It means the object gets destroyed as soon as
            it's not needed any longer.


            > And while on the topic, which is better?
            >
            > <cfobject name="MyComponent" component="cfcs.my.component">
            > or
            > <cfscript>
            > createObject("MyCompoent","cfcs.my.component")
            > </cfscript>
            >
            > Again, what are the benefits and drawback of each. I am more familiar with
            > the first using <cfobject> but I see the second more and more.

            For the purposes of your example? There's very little difference. I'd
            NEVER use <cfobject> for CFC stuff, in favour of <cfinvoke>, as it's more
            flexible.

            The one benefit createObject() has over either of the tags as it enables
            method-calling to be chained to the object creation call:

            myResult = createObject("component",
            "myCfc").init(myArg="foo").myMethod(myArg="bar");


            From an aesthetic perspective, I think CFML tags only really belong in
            situations where they are intrinsically working with HTML tags (looping out
            output repeating HTML, for example), which I think <cfinvoke> or <cfobject>
            very rarely are. I like to keep my code well away from my presentation, so
            most of the code I write never has anything to do with HTML, so tags are
            almost always the "wrong" answer.

            --
            Adam
            • 3. To create an object or not?
              WestSide Level 1
              Hi,

              I would agree with Adam as well. Many CF programmers use cfcs as more of a code organization tool and essentially a file full of related functions. Even though CF is not an OO Language, it does borrow some of the concepts at least that is what I think.

              Things like inheritance, encapsulations, etc can be achieved, you can get into having the standard beans and data access objects, so you might have a Person.cfc and a PersonDAO.cfc that would handle Create, Read, Update, Delete functions for that bean.

              I also like craig have some persistent objects that I load at Application Start as well as how he mentions his code jives with everything else or whatever..... writing in the more script like syntax which I believe is based on ECMAscript is similar to actionscript and javascript as well so with all the Web 2.0 buzz whatever Web 2.0 "really is", I think it behooves one to write code in script where possible so when you have to write some javascript or actionscript your good to go. One developer told me he uses cfscript for his business logic primarily.

              On a display page I would use a <cfloop> tag as opposed to doing some "for loop" in cfscript and using writeOuput() to display in an html table. <cfscript> can also be shorter. If you have to set a bunch of default variables in a cfc I think a cfscript block is cleaner, its shorter, just looks better, and may perform faster in some cases, but I am not sure if that is proven, a lot of factors can affect what would appear to be application performance.

              I rambled, but hopefully you can find some value in the comment and the others as well. This forum is not as active as it used to be, but it has helped me a great deal and I am still learning.

              In fact, now that I think about it, Adam had posted responses to some of my questions when I was initially getting into CF. (Not suggesting your getting into CF)

              -Westside
              • 4. Re: To create an object or not?
                Level 7
                Thank you for your update. I am in the process of implementing various
                patterns to some of my development (Factory, DAO, Gateways, etc). I see the
                value of scripting. I am constantly script AS2 and 3, but never made the
                connection to CF until recently. I was using mostly services based CFCs but
                am moving more always from that separating the business logic from the
                presentation layer more and more.

                Storing Objects in the application is a great new way for me to work. I
                would like to thank Craig for that. I have been storing certain persistent
                variable and data in the application for a while and never made the
                connection to objects till his post.

                Thank you for your post. I have been ravaging people's forums and blogs to
                get better at implementing patterns into my CF development.