Useless Getters/Setters
Posted by Jesse Freeman | Filed under Advanced Topics
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.
-
ryapley
-
FlashBum
-
Jodie O'Rourke
-
FlashBum
-
Kris












