PDA

View Full Version : calling getters/setters inside the constructor? best practice?



scottc
December 18th, 2008, 09:52 AM
What is the best way to use the getters/setters inside of a constructor?



//a simple class that draws a line from the origin...
package au.com.scottcampbell.draw
{
import flash.display.Sprite;

public class line extends Sprite{

//Private vars
private var _c:uint;//color
private var _a:uint;//alpha
private var _w:Number;//width
private var _h:Number;//height

public function line(nw:Number = 0,nh:Number = 0,nc:uint = 0x000000,na:uint = 100){
a(na);//set the alpha
c(nc);//set the color
w(nw);//set the width
h(nh);//set the height

render();//we want it rendered off the bat? yes
}
public function render():void{//update the graphics
graphics.clear();
graphics.lineStyle(0,_c,_a*0.01); //calculate the alpha into a decimal
graphics.moveTo(0, 0);
graphics.lineTo(_w, _h);
}

// Getters/Setters
public function set a(n:Number):void{
if(n > 100){//make sure the alpha is less then 100%
_a = 100;//should i cap this to 255 instead?
}else if(n < 0){//make sure its greater then 0%
_a = 0;
}else{
_a = n;
}
//render(); //should we render it once we have set the a value?
}
public function get a():uint{
return _a;
}
//...etc..etc...


I'm trying to improve my oops/as3 so try be as critical as possible. :P

Also whats a good naming convention for the parameters for the constructor. I start them all with "n" for new. (which i dont feel is a good way of doing things)

dail
December 18th, 2008, 03:23 PM
Why bother recalculating the alpha from 100 down to a decimal. Seems easier to just keep it as its native Number format from the outset.

For naming, I usually simply name the constructer params the same as the var I want them to give a value to?

scottc
December 18th, 2008, 11:42 PM
Why bother recalculating the alpha from 100 down to a decimal. Seems easier to just keep it as its native Number format from the outset.

For naming, I usually simply name the constructer params the same as the var I want them to give a value to?

Thanks for the tip about the alpha..

Before i never had the _privatevar naming convention, and i used nparam, can't believe i didnt see that. (i guess i was doing this at 2:30am)

what about using the getters in setters in the constructor? is it good/ok practice? it seems weird passing all the vars to functions just to cap and set them.

substance
December 19th, 2008, 03:45 AM
Here's (http://www.gamepoetry.com/blog/wp-content/uploads/2008/04/urbansquall_coding_convention.pdf) a good coding convention structure. I dont follow it strictly but it's a good jump-off for making your own standard. Personally, I start all parameter variables with an underscore(_) because all my read-only variables are named that way.

FYI, getters and setters are extremely slow in AS.

scottc
December 19th, 2008, 06:06 AM
Here's (http://www.gamepoetry.com/blog/wp-content/uploads/2008/04/urbansquall_coding_convention.pdf) a good coding convention structure. I dont follow it strictly but it's a good jump-off for making your own standard. Personally, I start all parameter variables with an underscore(_) because all my read-only variables are named that way.

FYI, getters and setters are extremely slow in AS.

Thanks for the link. :D


Member variables (marked with m_) should only be modified inside the class that owns
them. This typically means avoiding getter and setter methods.
function attackPlayer() : void
{
//NO NO NO
var p : Player = new Player();
var maxHp : int = p.getMaxHP();
var damage : int = p.getMaxHP() * .10;
p.takeDamage( damage );
//yes!!!
var p : Player = new Player();
p.takeDamagePercent( 10 );
}

It says just to typically avoid them, so im guessing they are still fine to use them and let the class do all the maths internally. aka be minimalistic with them.

Are they really that slow?
I don't see anyway around them without breaking encapsulation.
(edit: other then writing a public method)

(been reading a book on as3 design patterns :lol:)

JonnyR
December 19th, 2008, 07:20 PM
Hi Scott, just a couple of observations, comments (sorry if they're too critical, but you did ask! ;))

First off, Class naming convention dictates that all classes should start with a capital letter, so instead of calling the class "line", I reccomend you go with "Line". This helps you differenciate between class definitions and class instances.

Secondly, I personally don't use AS "soft" getters and setters, instead I make use of java style mutator methods, so for example:


public function getName() : String
public function setName(value : String) : void

I find this takes some of the "magic" out of the code making it easier for another developer to see what's going on at a glance.

As for naming the constructor params, I often go with the same name as my member properties, for example, a typical constructor in a class of mine would look something like this:


public class Car
{
private var numWheels;
private var colour

public function Car(numWheels : uint, colour : String)
{
this.numWheels = numWheels;
this.colour = colour;
}
}

YMMV :)