getting and setting  scott evans :: flex builder :: omgwtffb4

accessor methods, actionscript, and you

What better way to kick off this blog than with its namesake? And some tasty minutia?

One of the first Flex Builder 4 feature specs I’m working on is Generate Getters/Setters. I considered this feature a nice, but small, enhancement. But Tim’s $100 Feature Test shows that in fact this is a feature lots of people are waiting for. Nice!

To help validate our spec, I’d like to hear how and when accessors are used in the wild:

  • Do you typically create accessors when you create member variables, or later as you need them?
  • Would you want to batch-create accessors for more than one variable at once, or do you usually only do it for one variable at a time?
  • How do you name your variables that don’t have accessors? Do you prefix them? (i.e. m_color, m_flavor)

(The typical Java pattern is to put every member variable behind accessor methods, but that’s not appropriate in AS. Still, I have a feeling that some developers do it out of habit.)

Your feedback will help me be sure we’re getting this right. Comment away!

(Disclaimer: Like the disclaimer page says, this post — and all future posts — should not to be taken as a promise of any feature’s inclusion in any Adobe product. But you knew that, right?)

24 Comments so far

  1. Brian Deitte May 9th, 2008 1:21 pm

    Great to hear Scott. Is the spec going to be released?

    - Later as I need them.
    - Batch create. I love Intellij’s version of this, which also allows you to generate a constructor.
    - I don’t, but I see a lot of both mVariable and _variable.

  2. Danny Patterson May 9th, 2008 1:29 pm

    I used to think that everything should be a in a method. I hated public properties, but with Binding I slowly changed my tune. Now I see the refactorability of public properties in AS3. So the only time I use accessor methods is 1) I need to do something when that property is set (like invalidateProperties or set a dirty flag) 2) my property is read-only or 3) I’m implementing an interface that defines accessor methods. Otherwise I’ll just leave them as public properties.

  3. Leif Wells May 9th, 2008 2:53 pm

    - I usually create them later
    - both batch and individually, please
    - we normally use “-” (i.e. private var _someFoo:Foo;)

    I am really going to enjoy this blog if you have the time to keep posting entries! Thanks.

  4. Jeff Schuenke May 9th, 2008 2:57 pm

    I create accessors as needed but it would be nice to be able automatically create them when needed. I put an _ before private variable names.

  5. Rob McKeown May 9th, 2008 2:59 pm

    It would be nice if there was am [Invalidate ("properties", "display list", "size")] meta tag (like [Bindable]) that you could put on a setter that allowed you to invalidate that stuff without forcing you to implement the methods. I find that I end up having to create a private variable and create the getter and setter methods just so I can invalidateProperties when it is set.

  6. Frank Alic May 9th, 2008 3:33 pm

    Creating accessors is one of the annoying things to me. Especially when writing prototypes. So i create them as needed and would be very pleased if there will be a batch create for the. Usually i prefix private vars with an underscore.

    Thumbs up for this new blog. Keep posting!

  7. anon May 9th, 2008 3:55 pm

    - later if required

    - I name public and private properties the same (unless its a backing var for a getter/setter).
    e.g :
    var foo

    - I use an underscore for variables backing a getter/setter. Makes it easier to convert a private var into a public var or a getter/setter later if required.
    e.g:
    var _foo
    get function foo()
    set function foo()

    - both one var at a time and batch

  8. Bjorn May 9th, 2008 6:14 pm

    * Do you typically create accessors when you create member variables, or later as you need them?
    As i need them. Especially in mxml.

    * Would you want to batch-create accessors for more than one variable at once, or do you usually only do it for one variable at a time?
    1 variable at time is fine. But being able to batch-create accessors at the Class Create screen would be cool.

    * How do you name your variables that don’t have accessors? Do you prefix them? (i.e. m_color, m_flavor)
    No.

  9. Jody Brewster May 9th, 2008 6:19 pm

    Wait!!! Just do code gen!! IntelliJ, look at IntelliJ. Start the copy machines!! ;) Templates! Templates! Templates! And while I’m at it refactoring could be better, todos, code folding.

  10. Mark Walters May 9th, 2008 7:23 pm

    - Later as I need them.
    - Batch create would be great, if you could define your variables and then select multiple ones to generate getters/setters for.
    - I prefix accessor properties with an underscore, but requiring a certain prefix is limiting and would only restrict and frustrate certain users. If you do end up requiring a prefix to base accessor creation on, that prefix should be customizable.

    I understand what Danny up above is saying regarding public properties but one thing to keep in mind is that, while Binding is great for Flex projects, many people still create ActionScript-only projects as well in which Binding is not supported.

  11. gse May 9th, 2008 7:50 pm

    Thanks for the answers so far, folks!

    @Brian: We won’t be releasing specs.

    @Jody: I have used IDEA for years. :) It has a generate get/set feature as well as templating. As for templates and FB4, you may have seen it on Tim’s $100 test….

    I’m surprised to hear the combination of “I generate them when I need to” and “I want to be able to do batch generates”. Can anyone cite a recent real-world case where you had to generate, say, 4 accessor pairs at once?

  12. John C. Bland II May 9th, 2008 9:29 pm

    Give us a nice blend between Eclipse (Java’s getter/setter window) and FlashDevelop (has a nice code generator in the latest beta).

    I create my getters/setters for every public variable when building a class.

    private var _blah:Type;
    public function get blah():Type{ return _blah; }
    public function set blah(value:Type):void{ _blah = value; }

    I type that for every variable I’m going to have public. If it is private only…of course I don’t write them.

  13. Campbell May 10th, 2008 5:22 am

    Arent Accessors slower in Actionscript? Though I realise the minute you make them bindable its doing the same behind the scenes.

    I normally do it as needed and as the class expands.

  14. Constantiner May 10th, 2008 10:42 am

    Why do you plan to release get/set generators as separate feature? Maybe it is more useful to implement code templates like in JDT? Sure it will be more than suffice.

    Thank you.

  15. Glenn Williams May 10th, 2008 2:52 pm

    use underscore for private vars with Get\Set
    create get/set when i make the class

    great to this this blog. hope he find time to keep on posting

    glenn
    tinylion development UK
    flex-ria.com

  16. Josh Tynjala May 11th, 2008 5:44 pm

    Campbell said, “Arent Accessors slower in Actionscript?”

    Yeah, maybe by a tiny, tiny bit. If you’re trying to optimize by skipping accessors, you’re wasting your time. They’re used all over Flex, and I highly doubt they contribute noticeable slowdown on projects of any size.

    gse said, “Can anyone cite a recent real-world case where you had to generate, say, 4 accessor pairs at once?”

    Any time I have a set of properties where I need separate values for “left”, “right”, “top”, and “bottom”. If padding values in Flex weren’t styles, they’d be a perfect example. I know I’ve done it for other values, but I can’t remember for sure off the top of my head. That’s just one example, though. I’m sure if I thought about it, I’d have more. I write many accessors at once all the time. I groan every time I have more than one to create.

  17. Tek May 12th, 2008 1:05 am

    * Do you typically create accessors when you create member variables, or later as you need them?

    I mostly know what I need when I implement it. So I need it when I create members variables.

    * Would you want to batch-create accessors for more than one variable at once, or do you usually only do it for one variable at a time?

    I want a select multiple member variables that could create one or infinite number of getter/setter depending on the number of member variables selected

    * How do you name your variables that don’t have accessors? Do you prefix them? (i.e. m_color, m_flavor)

    I prefix them with an underscore but I want a field in editor options that the user could choose the form of the generated variable.

    Many thanks to ask for it.

  18. EgorKDie May 12th, 2008 4:29 am

    We create the accessors when we add a variable, so it would be nice to write all the variables and then create the accessors in a batch.

    We use underscore to prefix any variables for which we have an accessor.

    Having the ability to mark for binding at the time of creating the accessors would rock.

  19. gSOLO May 12th, 2008 7:17 am

    * Do you typically create accessors when you create member variables, or later as you need them?

    I create them when building the class.

    * Would you want to batch-create accessors for more than one variable at once, or do you usually only do it for one variable at a time?

    I currently do it one var at a time, but it would be convenient to be able to to it in a batch.

    * How do you name your variables that don’t have accessors? Do you prefix them? (i.e. m_color, m_flavor)

    Double underscore for private vars.

    private var __foo : Foo;
    public function get foo () : Foo {
    return __foo;
    }
    public function set foo (value : Foo) : void {
    __foo = value;
    }

    private var __bar : Bar;

    protected var fooBar : FooBar;

  20. Brian Holmes May 13th, 2008 9:57 pm

    I create them later. I really like the c# syntax for getters and setters. It’s much more smooth than java, with less code for when you do have to use them.

  21. Richard Rodseth June 22nd, 2008 7:39 am

    I use the underscore prefix. Most often I am adding [Bindable] metadata with a custom event, so it would be nice to incorporate that into the interface as well. In addition to getters/setters, the ability to add method overrides would be nice as well.

  22. Dale Bronk June 22nd, 2008 1:30 pm

    I create them as I need them. I only prefix my private variables with an underscore “private var _foo : String;” when I have getter/setters defined. If I do not have getters or setters defined I do not use the underscore “private var foo : String;”.

    Yes this does create an extra step when I add a get/setter in that I have to also rename my private variable by adding the underscore, but I do this for two reasons.
    1. I hate typing the underscore.
    2. This tells the developer right away that there is a get/setter for the variable when it is prefixed with the underscore.

  23. Jim June 23rd, 2008 8:01 am

    Prefixes are (traditionally) considered harmful.

    OK, I know that some of you will consider that statement a ‘religious’ viewpoint, but this issue has been studied since, at least, the days when ‘C’ was the king of programming languages.

    The problem with adding a prefix to variables is that it makes it much more difficult to find a particular variable, by name, in a list of variables. Our brains are highly trained to recognize words by _shape_ first and by individual letters secondarily. And for western natural languages as well as all programming languages that I can think of, the scanning pattern is left-to-right. Therefore, the shape (and letters) at the beginning of a word are more critical than those at the end.

    If we add a prefix to variables, then we modify the most critical part of the variable w.r.t. scan-ability and recognition.

    Is this issue only a problem for archaic tools like hard-copy variable cross-reference listings? No!
    You’ll encounter this as soon as you hit a breakpoint in the Flex IDE’s debugger. If you look in the Variables tab, you will see a list of the in-scope variables. Typically this can be a very long list, when you have in-scope variables with deep object graphs or lots of properties.

    It looks like that list is sorted by variable name. It also appears that the sort routine knows about the common Flex/AS3 practice of prefixing variable names with an underscore character. So, the good news is that using a single underscore prefix still results in a correct alpha sort. But the sort routine would have to know about whatever prefixes are being used (i.e., ‘_’, ‘m_’, etc.) to continue working robustly.

    ——
    Proposals (in decreasing order of desirability):

    A) Don’t use prefixes (or suffixes) at all.

    One consequence of this is that programmers may have to use “this.foo = foo” style syntax if they want to have their accessors use the same name as the private variables they are encapsulating.

    B) Use *suffixes*, not prefixes.

    For example, instead of: ‘m_foo’, use: ‘foo_m’ or ‘foo_’.

    C) Use the least (visually) obtrusive prefix possible.

    Specificially, use: ‘_foo’, not: ‘m_foo’.

    Thanks for listening, let the flames begin . . .
    Jim

  24. Tom Chiverton June 24th, 2008 2:31 am

    I normally start of with Bindable public var’s and later covert to set/get if needed.
    If there was a batch wizard, I’d probably use that directly rather than typing and then replacing a Bindable.
    I don’t normaly prefix or have any particular patten for public properties, get/setter or not. I try to make sure the private variable is prefixed with an underscore, then the name of the public property though (unless the setter wraps many properties).

Leave a reply