I read this post on what not to do with private properties. It’s for Java, but applies to Flash as well. Apparently declaring a property is the “amateur” way to do it. This is how I would do it.

public class Person {
private var _age:int;
public function Person(age:int) {
_age = age;
}
public function birthday():void {
_age++;
}
}

According to this fellow, that’s bad practice. You should always use accessors! Why write short concise code when you can write three times as much? Instead of age, use getAge() and setAge() because apparently its a lot better to write setAge(getAge()+1), rather than write age++.

Flash’s accessors would allow you to write age++, but this is still the most disturbingly defensive code I’ve ever seen. It’s a private property! A class is supposed to be able to manage its own properties. It’s other classes you’re supposed to mistrust, not your own. If a class has become so big it requires this kind of nonsense that’s a pretty good sign it should be split up into smaller classes.

In my opinion his only valid complaint is that its easier to make breakpoints. Fortunatly most of the comments for that article disagree with his opinion.

One Response to “Private properties need accessors?”

  1. Keith Peters Says:

    Well, the pages of comments that follow his post seem to agree with you. 🙂

Leave a Reply