This morning I received an email from Jason, whom I met at CF.United():
We are working to adopt/increase OO in our environment. We were debating over what may be best practice for object properties as far as ColdFusion components are concerned. For example:<cffuntion name="init" ...> <cfset this.foo = "bar">
Vs.<cffuntion name="init" ...> <cfset variables.instance.foo = "bar">
From the conference, conventions seemed mixed. We understand from blogs the benefits of the later example from a practice standpoint in mirroring OOP. However, are you aware of any performance advantage or future directions of either? One of use remembered hearing something particular about using “this”.
First, allow me to give a little background for those who may be new to the concept. In the same way that ColdFusion provides a plethora of scopes including the variables, request, session, and application scopes, CFCs provide two scopes of their own: “this” and variables.
The “this” scope is public. Anything can set values into this scope from outside the component. Essentially, it treats a component like a structure with methods. The result is that you have no protection over the data in your component. Programmers can accidentally (or intentionally) add bad data into your CFC which may break it or change its behavior in ways you do not want or intend.
In CFCs the variables scope is private. Only the component itself can write to the variables scope. This allows you to hide the component’s data and control and validate access via methods on your component. Getters and setters are typically created for this purpose. This also allow you to do more than just set values. For example, let’s say your component had an email address property. When you call setEmailAddress(), the setter could set variables.emailAddress, but it could also split the string on the @ sign and set variables.username and variables.domain as well.
Well, to directly answer your question, use of the “this” scope is faster. Presumably, this is because each function in a CFC is compiled down to a Java class. Orchestrating all the Java classes required for a CFC instance may add some overhead. In fact, In Model-Glue 3 Joe is actually preferring the use of the this scope due to the performance benefits and the ease of programming.
I’m still a fan of the variables scope with getters and setters. I like the notion that, if I need to, I can change the implementation of these functions. So, if I initially had an email address property that was accessed via a getter and setter I could add validation and/or the extra logic to set the username and domain without breaking the rest of my system. If I were using the this scope I would be unable to do that without modifying any code that touches the CFC. But, I pay a price for that in the time it takes to write the extra code and the overhead that extra code adds.
Beyond that, the use of “instance” variable in the variables scope is not even really a convention. Some people like it because they can now copy variables.instance out of the component easily. I don’t tend to do this.