Skip to main content
Inspiring
April 1, 2013
Answered

The Application.cfc's THIS and VARIABLES scopes

  • April 1, 2013
  • 1 reply
  • 866 views

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.

    This topic has been closed for replies.
    Correct answer Dan_Bracuk

    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.

    1 reply

    Inspiring
    April 1, 2013

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

    Inspiring
    April 1, 2013

    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?

    Dan_BracukCorrect answer
    Inspiring
    April 1, 2013

    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.