Check out our Angular Book Series.

Instance Variables in Flex and ColdFusion

There is one thing I really like about Flex classes.

They have real properties (AKA Instance Variables). ColdFusion doesn't have properties in the way that Java or .NET (or ActionScript) do. It often feels like a limitation.

This Variables Scope

If we were to create a CFC, there are two ways you can create instance variables. One approach is to use the 'this' scope. The second is to do so in the 'variables' scope. Take a look at this CFC:


<cfcomponent>

<cfscript>
this.foo = "bar";
variables.bar = "foo"
</cfscript>

</cfcompoment>

We created 'foo' in the this scope and bar in the variables scope. What is the difference between the two? Let's find out. First we need an instance of this CFC:


MyObject = Createobject('component','MyCFC');

Let's experiment with the foo variable to see what it can do. We can access it using object property notation, like this:


MyObject.foo = 123;

But, what if the component is expecting Foo to be a string and not a number? This direct access change it's type. We probably screwed up some internal process that will cause trouble later. There is no way to protect CFCs instance variable in the this scope. However, if you do this:


MyObject.bar = 123;

You won't affect the value of the private CFC instance variable bar. That's a good thing. It's inherently protected. But, what is bad is that there is no way to access the value of the bar variable from outside the component. The solution in the CF Community has been to write custom get and set methods. These methods are good because we have information hiding and other benefits of encapsulation. It's bad because it's tedious to write numerous get and set methods. Instead of using:


MyObject.bar = 123;

We would use:


MyObject.setbar(123);

To change the value of the instance variable. In the set method we could even add error checking to make sure that the value stays as a string, or number, or whatever.

Public and Private in Flex

Flex has a better way to handle instance variables. I think that will be more familiar to developer's of Java or .NET. You create instance variables in an ActionSCript class for use in Flex. When you do so you specify whether this variable is public or private. The code might look like this:


public var foo = 'bar';
private var bar = 'foo';

Public variables can be accessed just like they were in the this scope of a CFC. Private variables operate just like they were in the variables scope of a CFC. So, what's the difference other than some syntactic sugar?

ActionScript is strongly typed, so you don't have to worry about people setting your public variables to use an unexpected type. You can't change a string to an integer, or an integer to a class. You're inherently protected.

Conclusion

Because of this built in protection, you don't usually end up creating get and set methods. Private variables are created because they are internal the component and don't need outside access. You rarely have to write those tedious get and set methods.

I wish ColdFusion would provide a way to truly define public and private variables within a CFC.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Bruce's Gravatar Jeff:

I'm not following your conclusion. As you demonstrate CF does provide a way to define public (the this scope) and private (the variables scope) variables in a CFC.

One of the key differences between CF and ActionScript 3.0 is strict typing. Some people like strict typing and some people don't.

However, strict typing doesn't mean that you should have public variables in your classes. Both ActionScript 3.0's programming guide and Java (which also employs strict typing) standards encourage developers to keep their class variables private and expose public set and get methods when necessary.

ActionScript 3.0 does provide a different way you can create get and set accessor methods. See: http://livedocs.macromedia.com/flex/201/html/04_OO...


Bruce
# Posted By Bruce | 2/14/07 7:57 AM
Jeffry Houser's Gravatar Bruce,

I guess I'm on the fence between strict typing and dynamic typing. They both have benefits. CF's implementation of instance variables always felt kludgy to me.

My problem with CF is that, using the this scope, you have no control over how people use the instance variable. As such, this, is almost never used.

With strict typing, as in ActionScript, you have a bit more control. I can see that there are still benefits from keeping things private and using getter and setters. But, most of the time why would you bother?
# Posted By Jeffry Houser | 2/14/07 9:17 AM
All Content Copyright 2005, 2006, 2007, 2008, 2009 Jeffry Houser. May not be reused without permission
BlogCFC was created by Raymond Camden. This blog is running version 5.9.2.002.