Skip to main content
Inspiring
October 26, 2012
Question

Using THIS VS. A function to return private VARIABLES scope vars VS. miscellaneous accessor

  • October 26, 2012
  • 2 replies
  • 1250 views

I have a component that has variables set into it's component-private VARIABLES scope, however some other components need access to read some of those variables (but not set anything)

Is it better to:

(1) just move those variables into the THIS scope of the component so they are available to other components

(2) create a function in it that takes a variable name as an argument and returns the associated VARIABLES-scoped variable value, or

(3) is there a different, better coding method out there that I'm not familiar with? 

Currently I'm doing the second thing, and it works, but I have a feeling that CF has a better way of doing this or there is a better coding practice.

    This topic has been closed for replies.

    2 replies

    BKBK
    Community Expert
    Community Expert
    October 26, 2012

    Aegis Kleais wrote:

    I have a component that has variables set into it's component-private VARIABLES scope, however some other components need access to read some of those variables (but not set anything)

    Is it better to:

    (1) just move those variables into the THIS scope of the component so they are available to other components

    No. You wish friends and family to have access to your home. Would you hang a copy of the key outside on the door, making it available to the general public? That is in essence what the This scope does to a variable.

    Is it better to:

    ...

    (2) create a function in it that takes a variable name as an argument and returns the associated VARIABLES-

    Yes. Ideally, the scope of the variable should be private to the component. If the component wishes to share the variable, it can do so by means of a function, for example, like this 

    function getKey () {

    return key;

    }

    Inspiring
    October 26, 2012

    I've nothing much to add other than to agree with what BKBK says.

    Also in the context of CF OOP, as soon as you expose something in the THIS scope, the object kinda loses control of it because there's no way to make a this-scoped value "final" or "constant", so the calling code can change it at whim (so not only are you hanging the house key outside, you're inviting people to change the locks, as it were).

    My practice is that it's sometimes convenient to expose values to the outside world in the THIS scope, but my policy is that once they're in the this scope, they are not "allowed" to be referenced in the code within the object. But on the whole the only access code has to the inards of my objects are via method calls.

    If CF had the notion of constants or making variables "final", I'd perhaps use the THIS scope more.

    --

    Adam

    Inspiring
    October 28, 2012

    The only other way I'm aware of that you can keep the values private to the function is to create some type of getter that can return the variable requested.

    Inspiring
    October 26, 2012

    You seem to understand the difference between the this scope and private variables.  Since you have a choice, do what's most appropriate for your own situation.

    Inspiring
    October 26, 2012

    I guess, if they are needed from other components, I can move them into the THIS scope.