Since the advent of AS3 I have been working like this:
private var loggy:String;
public function getLoggy ():String
{
return loggy;
}
public function setLoggy ( loggy:String ):void
{
// checking to make sure loggy's new value is kosher etc...
this.loggy = loggy;
}
and have avoided working like this:
private var _loggy:String;
public function get loggy ():String
{
return _loggy;
}
public function set loggy ( loggy:String ):void
{
// checking to make sure loggy's new value is kosher etc...
_loggy = loggy;
}
I have avoided using AS3's implicit getters/setters partly so that I can just start typing "get.." and content assist will give me a list of all my getters, and likewise for my setters. I also dislike underscores in my code which turned me off the implicit route.
Another reason is that I prefer the feel of this:
whateverObject.setLoggy( "loggy's awesome new value!" );
to this:
whateverObject.loggy = "loggy's awesome new value!";
I feel that the former better reflects what is actually happening in the code. I am calling functions, not setting values directly.
After installing Flash Builder and the great new plugin SourceMate (which helps to get some of the useful features that FDT is famous into FB) I realized that when I use SourceMate's "generate getters and setters" feature it automatically sets my code up using the implicit route:
private var _loggy:String;
public function get loggy ():String
{
return _loggy;
}
public function set loggy ( loggy:String ):void
{
// do whatever is needed to check to make sure loggy is an acceptable value
_loggy = loggy;
}
I figure that these SourceMate people must know what they are doing or they wouldn't be writing workflow enhancement plugins for coding in AS3, so now I am questioning my ways.
So my question to you is: Can anyone give me a good reason why I should give up my explicit g/s ways, start using the implicit technique, and embrace those stinky little _underscores for my private vars? Or back me up in my reasons for doing things the way that I do?
To be honest I think this is a lot like indenting or brace style - where the importance/helpfulness of matching your style to whatever codebase you're working with eclipses any "inherent" advantage to either approach. With that said though, which of these would you rather maintain in a physics engine?
// with getters
body.position.y += body.velocity.y * dt;
// without
body.getPosition().setY( body.getPosition().getY() + body.getVelocity.getY() * dt );
Another advantage to getters/setters is that you can always make properties simple public variables initially, and refactor them into getters/setters later if needed, without changing external code. You don't have to preemptively build accessors for every variable; you can wait until you decide you need them.
I can think of a few reasons off the top of my head.
Keep in mind that if all you're doing is straight get/set, there isn't a whole lot lost by just exposing a public var and bypassing the getter/setter with the holding var (_variable). You can always change to an implicit get/set later without changing the classes external interface.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With