Naming Conventions

Naming Conventions

The naming conventions specified here apply only to Java code written in the basic ASCII character set. Terms such as " upper-case " are obviously meaningless for some Unicode character sets.

Package Naming

Generally, package names should use only lowercase letters and digits, and no underscore . Examples:

 java.lang        java.awt.image        dinosaur.theropod.velociraptor 

An exception to this rule is when using the unique package prefix scheme for packages that will be widely distributed. In this scheme, a unique prefix is constructed by using the components of the Internet domain name of the host site in reverse order. The first component (top-level Internet domain) is all upper-case, and the remaining components of the prefix are in whatever case they are conventionally written in. Example:

 COM.Sun.sunsoft.tools.graphics 

Class/Interface Naming

All type names (classes and interfaces) should use the InfixCaps style. Start with an upper-case letter, and capitalize the first letter of any subsequent word in the name, as well as any letters that are part of an acronym. All other characters in the name are lowercase. Do not use underscores to separate words. Class names should be nouns or noun phrases. Interface names depend on the salient purpose of the interface. If the purpose is primarily to endow an object with a particular capability, then the name should be an adjective (ending in -able or -ible if possible) that describes the capability, e.g., Searchable, Sortable, NetworkAccessible. Otherwise use nouns or noun phrases.

Field Naming

Names of non-constant fields (reference types or non-final primitive types) should use the infixCaps style. Start with a lowercase letter, and capitalize the first letter of any subsequent word in the name, as well as any letters that are part of an acronym. All other characters in the name are lowercase. Do not use underscores to separate words. The names should be nouns or noun phrases. Examples:

 boolean resizable;        char    recordDelimiter; 

Names of constant fields (final, primitive type fields) should be all upper-case, with underscores separating words. Remember that all interface fields are inherently final. Examples:

 MIN_VALUE, MAX_BUFFER_SIZE, OPTIONS_FILE_NAME 

One-character field names should be avoided except for temporary and looping variables . In these cases, use:

 b for a byte         c for a char         d for a double         e for an Exception object         f for a float         g for a Graphics object         i, j, k, m, n for integers         p, q, r, s for String objects 

An exception is where a strong convention for the one-character name exists, such as x and y for screen coordinates.

Avoid the character l ("el") in non-word variable names because it is hard to distinguish it from 1 ("one") on some printers and displays.

Method Naming

Method names should use the infixCaps style. Start with a lowercase letter, and capitalize the first letter of any subsequent word in the name, as well as any letters that are part of an acronym. All other characters in the name are lowercase. Do not use underscores to separate words. Note that this is identical to the naming convention for non-constant fields; however, it should always be easy to distinguish the two from context. Method names should be verbs or verb phrases. Examples:

 // GOOD method names:        showStatus(), drawCircle(), addLayoutComponent()        // BAD method names:        mouseButton()        // noun phrase; doesn't describe function        DrawCircle()                    // starts with upper-case letter        add_layout_component()        // underscores 

A method to get or set some property of the class should be called getProperty() or setProperty(), respectively, where Property is the name of the property. Examples:

 getHeight(), setHeight() 

A method to test some boolean property of the class should be called isProperty(), where Property is the name of the property. Examples:

 isResizable(), isVisible() 

Statement Label Naming

Statement labels can be targets of break or continue statements. They should be all lowercase, with words separated by underscores. Example:

 for (int i = 0; i < n; i++) {            search: {                for (int j = 0; j < n/2; j++) {                    if (node[j].name == name)                        break search;                }                for (int j = n/2; j < n; j++) {                    if (node[j].name == name)                        break search;                }            } //search        } 


Software Development. Building Reliable Systems
Software Development: Building Reliable Systems
ISBN: 0130812463
EAN: 2147483647
Year: 1998
Pages: 193
Authors: Marc Hamilton

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net