Flex best practices – Part 2: Development practices
ActionScript 3.0 coding standards
When authoring the source code for your application, there are several practices that you can use to help keep an application's codebase organized and readable. These will help other developers work on the application later. They can also help you remember how code works if you are required to modify the code in the future.
Best practices for using packages in development
Don't use verbs, adjectives, or adverbs for package names
These should be avoided when naming packages. Use pluralized or concept nouns.
Use plural names for packages
The name for a package containing multiple classes should be pluralized, for example, "effects".
Name packages according to the classes they contain
For example, if your application contains a collection of custom button classes you could create a package named "customButtons" for them.
For more best practices for packages see Flex best practices – Part 1: Setting up your Flex project. For additional practices to consider when creating packages, see Flex SDK coding conventions and best practices.
Best practices for classes
Minimize the amount of executable code defined in the class body
Keep the executable code encapsulated in methods.
Match instance variables to arguments
If the constructor was created to accept arguments and they set instance variables then you should match the argument names to the internal property names.
Follow the classic general conventions when creating classes
Class names should be singular objects within your application. Objects should have singular-noun-based names in UpperCamelCase. For example, if I were writing an online shopping cart system I might name two of the classes ShoppingCart.as and Customer.as. You should also avoid particularly large classes.
Append class types (formatter, validator, event, and error) to the class name
For formatter, validator, event, and error related classes append the type of class to the end of the class name. For example, use ServiceConnectionError, ServiceConnectionEvent, ServiceResultsFormatter, and ContactFormValidator.
Append the skin type to the class name
When creating classes related to skinning, append the type of class to the end of the class name. For example, use SubmitButtonBackground, SubmitButtonBorder, and SubmitButtonIcon.
Consider appending "Base" to superclass names
For class relationships that use inheritance consider appending "Base" to the class name. This can be seen in the Flex framework in classes such as ListBase. This practice is followed when the superclass is considered abstract.
Use blank lines in between methods
All methods in the class should be separated by a blank line. This increases code scannability and code readability.
Code to an interface when possible
If the class you are creating will be part of an inheritance relationship, try to create your class as a concrete representation of an interface. In this context, interface refers to an ActionScript interface, not the user interface.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)