Useless Getters/Setters
April 26th, 2008
Part of building my new Framework revolves around optimization (Code, Size, and Speed). One of the quickest optimizations I have done is removing all the useless getters and setters and instead replace them with public vars.
I use to be a big believer in always using a getter/setter to access class variables. In my mind it kept the class protected from other classes and allowed me to bake in additional logic into the getters/setters to validate the data passed into them. Well when I went back over my code and saw I had dozens of these things that simply exposed a private variable and did little else I realized how much of a wast that really was.
For a few years I never used a single public variable, now I find myself going back and once I remove the getters and setters, I can simply turn the private variables into a public one and the code rarely needs any more re factoring. Lets see an example:
This is my AS 3 Config class I posted a little while ago here.
import flash.events.EventDispatcher;
/**
* @author jessefreeman
* Use this as a base class for any configuration objects. It has built in support for a few basic values but should be extended to meet the needs of your application.
*/
public class AbstractConfig extends EventDispatcher {
/** Some Default variables **/
protected var _name : String;
protected var _x : Number;
protected var _y : Number;
protected var _width : Number;
protected var _height : Number;
protected var _alpha : Number;
protected var _rotation : Number;
protected var _debug : Boolean;
/**
* Get function for _name.
* @return _name : String
*/
public function get name () : String {
return _name;
}
/**
* Set function for _name.
* @param value : String
*/
public function set name (value : String):void {
_name = value;
}
/**
* Get function for _x.
* @return _x : Number
*/
public function get x () : Number {
return _x;
}
/**
* Set function for _x.
* @param value : Number
*/
public function set x (value : Number):void {
_x = value;
}
/**
* Get function for _y.
* @return _y : Number
*/
public function get y () : Number {
return _y;
}
/**
* Set function for _y.
* @param value : Number
*/
public function set y (value : Number):void {
_y = value;
}
/**
* Get function for _width.
* @return _width : Number
*/
public function get width () : Number {
return _width;
}
/**
* Set function for _width.
* @param value : Number
*/
public function set width (value : Number):void {
_width = value;
}
/**
* Get function for _height.
* @return _height : Number
*/
public function get height () : Number {
return _height;
}
/**
* Set function for _height.
* @param value : Number
*/
public function set height (value : Number):void {
_height = value;
}
/**
* Get function for _alpha.
* @return _alpha : Number
*/
public function get alpha () : Number {
return _alpha;
}
/**
* Set function for _alpha.
* @param value : Number
*/
public function set alpha (value : Number):void {
_alpha = value;
}
/**
* Get function for _rotation.
* @return _rotation : Number
*/
public function get rotation () : Number {
return _rotation;
}
/**
* Set function for _rotation.
* @param value : Number
*/
public function set rotation (value : Number):void {
_rotation = value;
}
/**
* Get function for _debug.
* @return _debug : Boolean
*/
public function get debug (): Boolean {
return _debug;
}
/**
* Set function for _debug.
* @param value : Boolean
*/
public function set debug (value: Boolean):void {
_debug = value;
}
/**
* Constructor
*/
public function AbstractConfig() {
//Add custom logic here if needed
}
}
}
vs
This is the config class I am now using:
public class AbstractConfig {
public var name : String = “untitled”;
public var x : Number = 0;
public var y : Number = 0;
public var width : Number = 0;
public var height : Number = 0;
public var alpha : Number = 1;
public var rotation : Number = 0;
public var debug : Boolean = false;
public var base_url:String = “/”;
/**
* Constructor
*/
public function AbstractConfig() {
// Does nothing
}
}
}
Big difference right? Sometimes the hardest part of optimizing is knowing where and when to “edit” your code. Here is a link to a really good article about the speeds of accessing variables from classes and how they range from getters/setters, sealed, dynamic and proxy classes.














April 26th, 2008 at 5:09 pm
In Action Script its no problem to use public vars, because at a later data it would be easy to switch to getters/setters. This is because AS3 is a ECMAScript and the external interface of a class is the same for public vars and get/set.
The reason that so many people do recommend to make getters/setters is that in for instance Java where you would have to find every call through the old public vars interface and translate to the new getters/setters interface.
April 27th, 2008 at 9:07 pm
Good to know, thanks for the added info.
April 30th, 2008 at 7:01 am
Your example is interesting, but there are times when you may not want to expose these members as public properties, for example, if you configured your app on start-up and then didn’t want to allow them to be altered after they had been assigned. In that situation, you’d pass them in as args on the constructor and then expose them through accessor methods. If they were public properties, they could be assigned new values at anytime.
May 1st, 2008 at 10:09 am
Jodie, I totally agree. This isn’t something I would suggest to do everywhere but I am interested in opening up the notion of optimization that breaks encapsulation to deal with real world pressures of development. Design Patterns are great and as ActionScript evolves they will play large role in our development but in this infancy stage a lot of use should question what the return is. Clients don’t care about how reusable your code is (most of the time) but they do care that the file is 10, 20, or 50k smaller because that return can cut hosting and bandwidth costs a lot. Thanks for the comment!
August 14th, 2008 at 1:46 pm
This is such an awesome discussion! I’m an artist more than a programmer and I love that I can take advantage of writing simple code for optimization. I do also agree there is a place for getters/setters.
Choice is the most important thing.