Java Developer Tools

Audit - Rules - Naming Conventions

Description
This group contains audit rules that check the names of various program elements for conformance to the naming standards that have been specified.

Rules:

Details

Accessor Method Naming Convention

Summary
The prefixes "get" and "set" should only be used for accessor methods.

Description
This audit rule checks for methods whose names begin with either "get" or "set" that are not accessor methods. A "getter" can have any number of parameters, but it must return a value. A "setter" must have at least one parameter and must not return a value.

Example
The following method would be flagged as a violation because it begins with the word "get", but does not return a value:

    public void getUserName()
    {
        ...
    }

Annotation Type Naming Convention

Summary
Annotation types names should conform to the defined standard.

Description
This audit rule checks the names of all annotation types.

Example
If the rule were configured to require that all annotation type names start with a capital letter, the following declaration would be flagged as a violation:

    public @interface internalUseOnly ...

Avoid Similar Names

Summary
Avoid names that are too similar.

Description
This rule detects pairs of names that are too similar and therefore can be confusing to read. Names that are spelled the same but are distinct because some letters have different case are too similar in most contexts. Using the singular and plural form of a word as separate names can also be misleading in some contexts. This rule also detects possible spelling errors where two identifiers are almost the same but have just a slight difference in spelling.

Example
    private String customername;

    public String customerName()
    {
        return customername;
    }

Boolean Method Naming Convention

Summary
Boolean method names should start with 'is', 'can', 'has' or 'have'. Non-boolean methods should not start with 'is'.

Description
This audit rule checks the names of all boolean methods to make sure that they are prefixed with 'is', 'can', 'has' or 'have'. It also checks the names of all non-boolean methods to make sure that they are not prefixed with 'is'.

Example
The following method would be flagged as a violation because it returns a boolean value but does not start with an approved prefix:

    public boolean willExplodeIfShaken()

The following method would be flagged as a violation because it starts with 'is' but does not return a boolean:

    public int isRoundedBy()

Class Naming Convention

Summary
Class names should conform to the defined standard.

Description
This audit rule checks the names of all classes to ensure that they conform to the standard.

Example
If class names are specified as needing to start with an upper-case letter, the following class definition would be flagged as being a violation:

    public class remoteDatabase
    {
        ...
    }

Consistent Suffix Usage

Summary
Type names should end with "Exception" if, and only if, they are derived from java.lang.Exception. Similarly, type names should end with "Error" if, and only if, they are derived from java.lang.Error.

Description
This audit rule finds types whose name ends with "Exception" but are not derived from java.lang.Exception, or classes that are derived from java.lang.Exception but whose name does not end in "Exception".

Example
The following class declaration would be flagged as a violation because the class name ends with "Exception" but it not derived from the class java.lang.Exception:

    public class RequiredClassException
    {
        ...
    }

The following class declaration would be flagged as a violation because it is derived from the class java.lang.Exception but does not end with "Exception":

    public class InvalidAuthorizationCode extends Exception
    {
        ...
    }

Constant Field Naming Convention

Summary
Constant names should conform to the defined standard.

Description
This audit rule checks the names of all static fields that are also final.

Example
If constant fields are specified as needing to consist only of upper-case letters and the underscore, the following declaration would be flagged as a violation:

    public static final int DefaultCount = 0;

Enumerated Type Naming Convention

Summary
Enumerated type names should conform to the defined standard.

Description
This audit rule checks the names of all enumerated types.

Example
If the rule were configured to require that all enumerated type names start with a capital letter, the following declaration would be flagged as a violation:

    public enum response {stronglyAgree, agree, disagree, stringlyDisagree, noOpinion}

Enumeration Constant Naming Convention

Summary
Enumeration constant names should conform to the defined standard.

Description
This audit rule checks the names of all enumeration constants.

Example
If the rule were configured to require that all enumeration constant names start with a capital letter, the following declaration would be flagged as a violation:

    public enum response {stronglyAgree, agree, disagree, stringlyDisagree, noOpinion}

Exception Parameter Naming Convention

Summary
Exception parameter names should conform to the defined standard.

Description
This audit rule checks the names of all exception parameters.

Example
If the rule were configured to only allow names of the form "e", the following parameter would be flagged as a violation:

    throw {
        ...
    } catch (Exception problem) {
        ...
    }

Instance Field Naming Convention

Summary
Instance field names should conform to the defined standard.

Description
This audit rule checks the names of all instance fields.

Example
If the rule were configured to only allow instance fields to begin with a lower case letter, the following declaration would be flagged as a violation because it begins with an upper case letter:

    private int MaxCount;

Interface Naming Convention

Summary
Interface names should conform to the defined standard.

Description
This audit rule checks the names of all interfaces.

Example
If the rule were configured to require that all interface names start with a capital "I" and another capital letter, the following declaration would be flagged as a violation because the name does not begin with a capital "I":

    public interface EventListener
    {
        ...
    }

Label Naming Convention

Summary
Label names should conform to the defined standard.

Description
This audit rule checks the names of all labels.

Example
If the rule were configured to require that labels begin with a lower case "l" and an upper case letter, the following label would be flagged as a violation because it does not begin with a lower case "l":

    here: while (true) {
        ...
    }

Local Variable Naming Convention

Summary
Local variable names should conform to the defined standard.

Description
This audit rule checks the names of all local variables (parameters and temporary variables).

Example
If the rule were configured to only allow local variables to begin with a lower case letter, the following would be flagged as a violation because it starts with an underscore:

    int _count;

Loop Variable Naming Convention

Summary
Variables declared in for loops should conform to the defined standard.

Description
This audit rule checks the names of all integer-valued loop variables to see whether they are on a list of approved names.

Example
If the rule were configured to only allow loop variable names of "i", "j" and "k", the following loop variable would be flagged as a violation:

    for (int index = 0; index < array.length; index++) {
        ...
    }

Method Naming Convention

Summary
Method names should conform to the defined standard.

Description
This audit rule checks the names of all methods.

Example
If the rule were configured to only allow method names that begin with a lower case letter, the following method declaration would be flagged as a violation because the method name begins with an upper case letter:

    public static Singleton GetInstance()

Method Parameter Naming Convention

Summary
Method parameter names should conform to the defined standard.

Description
This audit rule checks the names of all method parameters.

Example
If the rule were configured to only allow parameter names that begin with a lower case letter, the parameter in the following method declaration would be flagged as a violation because it begins with an underscore:

    public void setCount(int _count)

Package Naming Convention

Summary
Package names should conform to the defined standard.

Description
This audit rule checks the names of all packages as defined by the package declarations at the beginning of each compilation unit.

Example
If the rule has been configured to only allow package names consisting of lower case letters, the package name "utilitiyClasses" would be flagged as a violation because it contains an upper case letter.

Package Prefix Naming Convention

Summary
Package names should begin with either a top-level domain name or a two-letter country code.

Description
This audit rule checks the names of all packages as defined by the package declarations at the beginning of each compilation unit to ensure that the first identifier is either a top-level domain name or a two-letter country code.

Example
For example, the following package declaration would be flagged as a violation because "freeware" is neither a top-level domain name nor a two-letter country code:

    package freeware.model;

Package Structure

Summary
Packages should follow a prescribed structure.

Description
This audit rule looks for package names whose structure does not conform to the configured standard.

Example
If the rule were configured to require package names to start with a prefix of "org.apache" and have at least one segment representing the name of the project, with "ant", "jakarta", and "struts" on the list, the name "justforfun" in the following package name would be flagged as a violation:

    package org.apache.justforfun.nonsense;

Pluralize Collection Names

Summary
The names of collection-valued variables should be plural.

Description
This audit rule checks for collection-valued variables whose name is singular. A collection-valued variable is any variable whose type is either an array type or a subtype of the type java.util.Collection.

Example
The following field declaration would be flagged because its name is singular:

    private List employee;

Questionable Name

Summary
Questionable names may indicate sloppy code.

Description
This audit rule checks for questionable names of variables, fields, methods, and types. A questionable name may indicate sloppy code.

Three types of questionable names are flagged:

1. Names that occur in a user-configurable list.

2. Names that are shorter than a user-configurable size. Short names can be useful as for loop variables or in catch clauses so short names are not flagged if they occur in those two places.

3. Names that are longer than a user-configurable size.

Names that you always want to be accepted can be added to a list at the bottom of the preference pane. These names will be accepted whether or not they break any of the 3 rules given above.

Example
The variable "c" would be flagged as a violation in the following method because it is too short, but the variable "i" would not be because it is used as a loop variable:

    public int getNonZeroElementCount()
    {
        int c;

        c = 0;
        for (int i = 0; i < elements.size(); i++) {
            ...
        }
        return c;
    }

Static Field Naming Convention

Summary
Static fields should have names that conform to the defined standard.

Description
This audit rule checks the names of all static fields that are not also final.

Example
If the rule has been configured to allow only names that begin with an upper case letter, then the following declaration would be flagged as a violation:

    public static int minutesPerHour = 60;

Type Names Must Be Singular

Summary
Type names must be singular.

Description
This audit rule checks for types whose name is plural.

Example
The following class declaration would be flagged because the name is plural:

    public class Employees
    {
        ...
    }

Type Parameter Naming Convention

Summary
Type parameter names should conform to the defined standard.

Description
This audit rule checks the names of all type parameters.

Example
If the rule were configured to require that all type parameter names start with a capital letter, the following declaration would be flagged as a violation:

    public class BetterCollection<elementType> ...

Use Domain-specific Terminology

Summary
Use terminology applicable to the domain.

Description
Avoid using generic terms in identifiers. Use terms appropriate to the domain instead. This rule maps generic terms to specific terms according to a user-supplied list.

Example
If the term "thingy" were added as a generic term for the domain-specific term "widget", the following class name would be flagged as needing to be replaced by "MyWidget":

    public class MyThingy
    {
    }

Use Type-Specific Names

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.