Implicit vs explicit getters / seters in AS3 which to use and why?

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 new value is kosher etc... this.loggy = loggy; } 

and 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 new value is kosher etc... _loggy = loggy; } 

I avoided the partial use of the implicit AS3 get / install methods so that I could just start typing “get ..” and the content assistant will give me a list of all my get methods, as well as my installers. I also don't like the underscores in my code that disconnected me from the implicit route.

Another reason is that I prefer to feel this:

 whateverObject.setLoggy( "loggy awesome new value!" ); 

to that:

 whateverObject.loggy = "loggy awesome new value!"; 

I feel that the former better reflects what is actually happening in the code. I call functions, not set values ​​directly.

After installing Flash Builder and the great new SourceMate plugin (which helps to get some useful features known to FDT in FB), I realized that when I use the “create and get methods” function of SourceMate, it automatically installs my code using an 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 believe that these people from SourceMate should know what they are doing, otherwise they will not write plugins to improve workflows for coding in AS3, so now I question my ways.

So, my question to you is: can someone give me a good reason why I should abandon my explicit g / s methods, start using the implicit technique and use these little smelly _underscores for my personal changes? Or support me in my reasons for doing what I do?

+4
source share
3 answers

Honestly, I think this is very similar to the indentation or curly brace style - where the importance / usefulness of matching your style with any codebase you work with outshines any "inherent" advantage for any approach. With that said, though, which one do you prefer to support in a physical engine?

 // with getters body.position.y += body.velocity.y * dt; // without body.getPosition().setY( body.getPosition().getY() + body.getVelocity.getY() * dt ); 

Another advantage for getters / seters is that you can always first make the properties of simple public variables and then reorganize them into getters / setters, if necessary, without changing the external code. You do not need to pre-create accessors for each variable; you can wait until you decide that you need them.

+19
source

I can think of several reasons from the head.

  • Implicit get / set offers better / simplified data binding functionality. It’s easier to hook up events and fits into the “Flex model” much nicer.
  • This makes your generated ASDoc more understandable.
  • This, I believe, is the only way to get a property in the Property inspector for custom components when you work in design.

Keep in mind that if all you do is get / set directly, then you won’t get lost a lot by simply exposing the public var and bypassing the getter / setter while preserving var (_variable). You can always change the implicit get / set later without changing the external interface of the classes.

+2
source

I do not use implicit getters, because if refactoring and code readability harden. As a rule, it is not so easy to detect if the assignment refers to a public field or if it is an access method. Therefore, if you want to track the error, you can get into an unpleasant hunt.

0
source

Source: https://habr.com/ru/post/1310691/


All Articles