Typical Programmer

A glittering performance of rare perception


Popular articles
All articles

About me
Hire me

Do as I say: More on bad sample code

09 Jun 2007

I came across this in the Flex 2.0 documentation:

You can define properties for your components by using setter and getter methods. The advantage of getters and setters is that they isolate the variable from direct public access so that you can perform the following actions:

  • Inspect and validate any data written to the property on a write
  • Trigger events that are associated with the property when the property changes
  • Calculate a return value on a read
  • Allow a child class to override

Here’s the example code from the docs:

// Define internal private variable.
private var _initialCount:uint = 42;// Define public getter.

public function get initialCount():uint {
    return _initialCount;
}

// Define public setter.

public function set initialCount(value:uint):void {
    _initialCount = value;
}

Now Flex’s scripting language, ActionScript 3, has this nice syntax for defining getters and setters that makes the getter/setter functions syntactically identical to accessing a public member variable. Using the example above, accessing initialCount looks like this:

var m = new whatever();
var ic = m.initialCount;
m.initialCount = 43;

So unless the getter does something more than return the value of a private variable, or the setter does something other than assign a value to it, there’s no reason to define a getter and setter as the example shows. The example makes a good case not to use a getter and setter. Just expose the public variable; if you ever need to wrap it in a getter and setter ActionScript will let you do it without disturbing any of the code using that variable.

Experienced programmers will probably understand this, but someone new to OOP or used to a language with less transparent getter/setter syntax will either not understand the point of getters or setters, or create them for every member variable thinking that’s how OOP is done.

A couple of better examples would show a getter that returned a calculated value, or a setter that validated or cleaned the value passed in.

A useful application not mentioned in the docs is making a member variable read-only by providing a getter but no setter.

The docs don’t make it clear if setters can be used in contexts other than straight assignment. Will m.initialCount += 3 work? A few examples would be helpful.

Examples in documentation can be more useful than the explanations, but only if they are good, thoughtful examples that show realistic coding problems and solutions. Too many product documents and books are full of examples that give no insight and can’t be transferred to real applications. Editors of programming books are often guilty of allowing the author to use example code snippets to show off or demonstrate geek humor, like the value 42 in the example above. The best programming books like The C Programming Language and Software Tools consist of many excellent examples that demonstrate solutions to real problems, not page after page of sample code that is not instructive and can’t be used in a real application.