5 Replies Latest reply on Apr 1, 2013 2:56 PM by Adam Cameron.

    The Application.cfc's THIS and VARIABLES scopes

    Aegis Kleais Level 3

      So traditionally the THIS scope stores variables that are used outside the component, and the VARIABLES stores, well, variables, that are private to the component, right?

       

      I ask, because in my application.cfc's BIFs (like onApplicationStart(), onRequestStart(), etc.), I make calls to other components, and there are some values that are initially stored in the application.cfc that need to be shared with these components (but I was told that I shouldn't just make these components inherit from the application.cfc)

       

      What I *WAS* doing was calling these component methods and passing the variables I *WAS* storing in the VARIABLES scope like the following:

       

      <cfset componentObject.methodName(
           firstArg = VARIABLES.var1,
           secondArg = VARIABLES.var2
      ) />
      

       

      Thing is that I've found, these variables are needed rather often by a lot of functions throughout the application.cfc.  Should I be storing these common values in the THIS scope instead?  Someone told me that it's sloppy to just send the entire THIS/VARIABLES structure as an argument that is not even defined in the function being called.

        • 1. Re: The Application.cfc's THIS and VARIABLES scopes
          Dan Bracuk Level 5

          I would keep them in the variables scope and pass them as arguments to the functions in the other cfcs.

          1 person found this helpful
          • 2. Re: The Application.cfc's THIS and VARIABLES scopes
            Aegis Kleais Level 3

            Hey Dan!  Thanks for the feedback.  May I ask why it is you choose to do it this way?  Is this a case of simple semantics?  Or have you found this to be the best practice for your personal experience?

            • 3. Re: The Application.cfc's THIS and VARIABLES scopes
              Dan Bracuk Level 5

              I choose the variables scope because the this scope allows applications using my cfc to change the values of the variables in their object.  I rarely if ever see a need to allow this.

               

              I suggest passing values as arguments because my primary use of cfc's is to produce re-useable code.  In our setup, (intranet with reuseable code available to all applications), it doesn't make sense for one cfc to use another cfc's variables directly.  It might work, but it just seems wrong to me.

              • 4. Re: The Application.cfc's THIS and VARIABLES scopes
                Aegis Kleais Level 3

                Very cool, Dan, thanks for the explanation.

                 

                Before, I was a bit one-sided in how I saw things.  I only thought of "THIS" and "VARIABLES" as being scopes to share data from one component to the other.  But I had forgotten completely about the "shared to" componet's ability to then MODIFY that data.  Once I comprehended that, I can see that the VARIABLES scope does seem to be the proper scope I want to use in this case.  Cause the data I'm sending is data defined in the first component, needed by the second, but should not be modified by the second, so VARIABLES sounds perfect; in that it is private to the component and not designed to be changed by components other than itself.

                 

                Much appreciated!

                • 5. Re: The Application.cfc's THIS and VARIABLES scopes
                  Adam Cameron. Level 5

                  Over and above what Dan says (which seems spot-on to me), if you're using these variables a lot in different methods of the same CFC, consider whether it's appropriate to initialise the CFC instance with those variables at the outset, and then storing them in its variables scope for re-use by its methods as needs must. This does, obviously, only work for non-transient objects.

                   

                  --

                  Adam

                  1 person found this helpful