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?)
28 Comments so far
Leave a reply
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.
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.
- 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.
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.
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.
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!
- 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
* 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.
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.
- 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.
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?
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.
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.
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.
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
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.
* 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.
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.
* 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;
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.
Visual Studio 2008 creates my event handler methods for me “magically”, and I expect the same from FB. I don’t see the functionality in v3 (am I missing it)? Is it slated for v4?
In almost all cases I use private variables with getters and setters, even if there is no logic behind assignment. Private vars are named with a “_” as a style preference, usually sharing the same name as the public getters/setters.
For about half of protected variables I do not generate getters and setters, but they all also almost always named with a “_” as a matter of style.
This is a carry over from Java, where I principally learned OOP practices.
But, in many cases a complex datatype will have many public attributes with error checking logic. Creating a dozen getters/setters is an arduous task that almost every developer has had to do at some point. A task that can be easily handled by a RegExp of what’s selected, and a loop.
The Java perspective has MANY excellent tools (in the Source drop down) that save developers hours and hours of work over the course of a large project. In my opinion making the Flex IDE emulate the Java perspective’s “Generate Getters and Setters” would make the most sense from both a consistency and functionality viewpoint.
It also wouldn’t hurt to pull out some other features from the Java perspective’s Source drop down, such as:
Override/Implement Methods…
Sort Members…
Format Element
Format
https://bugs.adobe.com/jira/browse/FB-11636
Hi Scott,
* Later as I need them,
* I want to batch-create accessors for more than one variable at once,
* I don’t prefix them.
Thank you for staying tuned,
Chris
I create accessors (getter/setters) when I create the private members (properties) of the class. It would be a great time-saver to be able to automate the process based on the name of the private variables in the class.
Also, one feature I’m dying to see is a way to collapse sections of code; my current MXML file is about 900 lines long. I can collapse the controls using the little (-) symbol; no such feature exists for functions. It would also be nice if we could define our own blocks that could then be collapsed/expanded.
I use them but most of the time I don’t need to see them, so I would like to be able to avoid stepping into every single setter/getter when debugging since it adds lots of extra steps. So, a feature I would like is an enable/disable of “step into” during debugging.
I agree with “Epoch of Entropy”.
One of the things that drives me crazy about FB3 is that it has almost all of the same concepts (or at least it should) as the JDT but it either implements them differently or features are simply missing.
Almost everything on the JDT editor view context menu is appropriate for as3 code and should behave the same way.
I do a lot of coding where I’m switching between Java and AS3 (developing backend with frontend) and it pains me that the JDT has really great refactoring tools but FB does not.
Don’t re-invent the wheel. Flash developers: stop bashing Java for no reason and realize that the two are very similar and should share toolsets whenever possible. If it’s not perfect, at least it’s consistent.
I just watched the video interview of you with Ryan S. And I thought I’d post my simple IDE enchanement here (note, it’s also up in the FB Jira as feature request FB-17375). Basically, I’m looking for quick access into the native operating systems file browser.
When exploring projects in the Flex Navigator view pane, it’s be very nice to be able to right-click (option-click) the name of the project (and or individual files) and have an option to open up the native operating system’s file browser in that location.
I find that right now i hvae to
1) right-click
2) bring up the properties window
3) copy the file path to the project
4) open a native file browser
5) paste the copied path into the file browser
6) use as needed.
I also find that do to many quirks with SubEclipse plugin for Subversion that I need to use Subversion tools in the native file browser all the time, and as a result I’m doing stpes 1 though 6 many times/day above.
Thanks.
I just watched your interview with Ryan. Good stuff. I’d request that there is an option in FB to chose the sort of code formatting the developer uses. I know that most follow the Java standard of putting { on the line after the method declaration or conditional. However I put the { on the same line.