4 Replies Latest reply on Mar 19, 2009 6:30 AM by apocalipsis19

    CFC Methods and Code

    apocalipsis19 Level 1
      Hello Community!

      I was asked to create a CFC that will encapsulate together the methods that as of today my company has in 3 different files. Each one of these file is included in the main program as a template using cfinclude. When I opened the 3 files, I noticed that the first 150 lines of code were exactly the same. The only thing that changed in the 3 of them were the methods after those blocks of code.

      Before I ask my question let me explain how I am laying out my code:

      <cfcomponent displayname="" hint="">

      code that was common in the 3 places
      code that was common in the 3 places
      code that was common in the 3 places
      code that was common in the 3 places
      code that was common in the 3 places

      method 1
      method 1 code
      method 1 code
      method 1 codd

      method 2
      method 2 code
      method 2 code
      method 2 code

      method 3
      method 3 code
      method 3 code
      method 3 code

      <cfcomponent>

      Ok, I want to know if everytime I call one of my methods the 'code that was common in the 3 places
      ' will get executed or if I have to put that code in another method that gets called everytime I call the others. I want an idea to complete this task. I have created CFCs before but this is the first time I face a situation where I have to make such decisions.

      Thanks!

      Ysais.
        • 1. Re: CFC Methods and Code
          Dan Bracuk Level 5
          The way you describe your code layout is ok. Whether or not you can improve upon it depends on what those 150 lines do.

          Whether or not those 150 lines get called each time you use a function depends on how you call them. If you use cfinvoke, they get executed each time. If you use createobject or cfobject, they only get executed once.
          • 2. Re: CFC Methods and Code
            apocalipsis19 Level 1
            Thanks Dan!

            I am having an issue now. All of my variables in the variables scope are nor being read in my methods; thus my program crashes.

            <cfcomponent displayname="" hint="">

            code that was common in the 3 places
            code that was common in the 3 places
            code that was common in the 3 places
            code that was common in the 3 places
            code that was common in the 3 places
            Declaration of Variables
            Declaration of Variables
            Declaration of Variables

            method 1
            method 1 code
            method 1 code
            method 1 codd

            method 2
            method 2 code
            method 2 code
            method 2 code

            method 3
            method 3 code
            method 3 code
            method 3 code

            <cfcomponent>

            I am declaring my variables in the variables scope at the top of my CFC as you can see in the sample representation of my code.

            Does it look weird or is that OK?

            Thanks!
            • 3. Re: CFC Methods and Code
              apocalipsis19 Level 1
              I figured this out by self too!

              Thank you Dan for your input!
              • 4. Re: CFC Methods and Code
                apocalipsis19 Level 1
                I figured this out by myself too!

                Thank you Dan for your input!