Flex best practices – Part 2: Development practices
Organize ActionScript classes
Keep ActionScript classes organized and arranged in a way that is consistent with the rest of the source code throughout your application. This will help later when you or another developer needs to locate a particular area of the code to make a quick change.
You can use the following structure as an example:
- Initial comment. (Author, version, copyright, code license, and so on).
- Package declaration
- Import statements
- Class-level metadata tags: Event, Style, Effect (with comments!)
- Class or interface implementation ASDoc comment
- Class or interface statement
- Getter/setter methods (with backing variables)
- Methods, grouped according to functionality
Indent each new block of code by four spaces
Set your editor to insert spaces instead of tabs when you press the Tab key. This will help avoid cross platform source code display and readability issues. Tabs are displayed differently on different operating systems.
Separate each method in a class with a blank line
Create each method with an ASDoc documentation comment directly above the method. Follow this with the method signature with typed arguments, if arguments are present, then the method body, and finally a blank line.
Use spaces to improve code readability
In general use one space after commas. Use spaces before and after operators such as + and -.
MXML coding standards
To help maintain consistency and order, use the following practices when authoring or editing MXML source code in a Flex application.Best practices for elements
Organize element attributes
Order the element attributes: property, events, effects, styles.
Place the ID attribute as the first attribute
For example: <mx:Button id ="thisButton" />
Group associated attributes together on one line
For example: paddingLeft, paddingRight, x, y, etc.
Group related attributes
If there are more associated attributes than will fit on one line, then use an additional line but keep the related attributes together as best as possible.Best practices for formatting
Use blank lines to organize MXML
Place a blank line between unrelated groups of MXML elements, such as elements defining transitions and an HBox that controls application layout.
Organize MXML documents
Use a consistent structure to organize your MXML; for example:
- XML Declaration
- Root XML element with namespaces used in XML document
- Metadata tags: Event, Style, Effect
- Style element, should link to an external CSS file
- Script tag; follow the standards outlined in this article in the Best practices for ActionScript class file organization section.
- Data related MXML elements, XML, Model, etc.
- Transitions elements
- MXML UI controls and containers
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)