AddThis Social Bookmark Button
 Naming conventions in actionscript (part 2)

Naming constants

You can use constants for situations in which you need to refer to a property whose value never changes. This helps you find typographical mistakes in your code that you might not find if you used literals. It also lets you change the value in a single place.

Variables should be lowercase or mixed-case letters; however, use the following guidelines for naming static constants (variables that do not change):

  • Constants should be uppercase.
  • Separate words should contain underscores.
You can see these guidelines at work in the following ActionScript code snippet:
var BASE_URL:String = "http://www.Adobe.com"; // constant
var MAX_WIDTH:Number = 10; // constant
Do not directly code numerical constants unless the constant is 1, 0, or -1, which you might use in a for loop as a counter value.

Naming Boolean variables

Start Boolean variables with the word "is" (because a Boolean value either "is" or "is not" because of its nature). Therefore, you might use the following for whether a baby is a girl or not (which is a Boolean value): isGirl Or for a variable indicating whether a user is logged in (or not), you might use the following: isLoggedIn

Naming functions and methods

Use the following guidelines when you name functions and methods in your code.
  • Use descriptive names.
  • Use mixed case for concatenated words. A good example would be singLoud().
  • Start function and method names with a lowercase letter.
  • Describe what value is being returned in the function's name. For example, if you are returning the name of a song title, you might name the function getCurrentSong().
  • Establish a naming standard for relating similar functions. ActionScript 2.0 does not permit overloading. In the context of object-oriented programming, overloading refers to the ability to make your functions behave differently depending on which data types are passed into them.
  • Name methods as verbs. You might concatenate the name, but it should contain a verb. You use verbs for most methods because they perform an operation on an object. Examples of method names include the following:
    sing();
    boogie();
    singLoud();
    danceFast();
Naming classes and objects

When you create a new class file, use the following guidelines when you name the class and ActionScript file. For proper formatting, see the following examples of class names:
class Widget;
class PlasticWidget;
class StreamingVideo;
You might have public and private member variables in a class. The class can contain variables that you do not want users to set or access directly. Make these variables private and allow users to access the values only by using getter/setter methods.

The following guidelines apply to naming classes:

  • egin a class name with an uppercase letter.
  • Write class names in mixed case when it's a compound or concatenated word. Begin with an uppercase letter for a compound or concatenated word. A good example is NewMember.
  • Class names are usually nouns or qualified nouns. A qualifier describes the noun or phrase. For example, instead of "member," you might qualify the noun by using NewMember or OldMember.
  • Clear names are more important than short names.
  • Don't use acronyms and abbreviations. The exception to this rule is if acronyms or abbreviations represent the standard way to use a term (such as HTML or CFM). For commonly used acronyms, use mixed cases such as NewHtmlParser instead of NewHTMLParser for improved readability.
  • Use meaningful and simple names that are descriptive of the class contents. To avoid being vague or misleading, use generic names. * Sometimes a class name is a compound word. A qualifier might describe the noun or phrase. For example, instead of "member," you might qualify the noun using NewMember or OldMember.
  • Do not pluralize the words you use in the class name (such as Witches or BaldPirates). In most cases, it is better to leave the words as qualified nouns instead. A qualifier describes the noun or phrase. For example, instead of "cat" or "buckaneer," you might qualify the noun by using BlackCat or OldBuckaneer.
  • Don't use a class name in the properties of that class because it causes redundancy. For example, it does not make sense to have Cat.catWhiskers. Instead, Cat.whiskers is much better.
  • Don't use nouns that also might be interpreted as verbs. For example, Running, or Gardening. Using these nouns might lead to confusion with methods, states, or other application activities.
  • Use unique class names for each class in a single application.
  • Do not name classes so that they conflict with the names of built-in classes in Flash.
  • Try to communicate the relationship that a class has within a hierarchy. This helps display a class's relationship within an application. For example, you might have the Widget interface, and the implementation of Widget might be PlasticWidget, SteelWidget, and SmallWidget.
Naming packages

It's common for package names to use "reverse domain" naming convention. Examples of reverse domain names include com.Adobe for Adobe.com, and org.yourdomain for yourdomain.org.

Use the following guidelines when you name packages:


  • Put the prefix for a package name in all lowercase letters. For example, com, mx, or org.
  • Put related classes (classes with related functionality) in the same package.
  • Begin package names with a consistent prefix. For example, you might use com.Adobe.projectName to maintain consistency. Another example would be com.Adobe.docs.learnAS2.Users for the Learning ActionScript 2.0 Reference.
  • Use a clear and self-explanatory package name. It's important to explain the package's responsibilities. For example, you might have a package named Pentagons, which is responsible for using the Flash drawing API to draw various kinds of pentagons in documentation examples;
    its name would be com.Adobe.docs.as2.Pentagons.
  • Use mixed capitalization for compound or concatenated package names. packageName is an example of a compound, concatenated package name. Remember to use all lowercase letters for the prefix (com, org, and so on).
  • Do not use underscores or dollar sign characters.
AddThis Social Bookmark Button
If you think this page is providing useful information, don't hesitate to leave a comment.

[ 1 comment ]

flashvalley
 
Copyright ©2006-2008 flashvalley.com - All rights reserved