Summary of the Java development naming specification

  • 2020-04-01 02:23:09
  • OfStack

Precautions before use:
1. Due to the nature of Java object-oriented programming, you should try to choose nouns when naming

2. Camel Case (Camel Case) : when the variable name or function name is a unique identifying word linked by one or more single words, the first letter begins with lowercase, and the first letter of each word is capitalized (except the first word).

Such as: myFirstName

Writing specification of a Package name (Package)
It is recommended that the package name be prefixed with the top-level domain of the company or organization in order to ensure the uniqueness of the package name used within each company/organization. Package names are all lowercase and have a practical distinguishing meaning.

1.1 general requirements
1. Choose meaningful names that quickly convey the purpose of the class.

2. All package names must be in lower case.

1.2 practical application
Layers are often applied in application systems, such as Dao layer (database access), Service layer (business processing), and Web layer (page control action class).

1, the first few of the package name for the fixed name, if it is a website, the use of the website of the domain name of the reverse write, if the domain name has not been determined, the use of the company fixed several names. Such as: net. Vschool

2. The next word after the package name is the name of the module. For example: user module, the package name is net.vschool.user

3. The access operation of the module adopts the hierarchical form, which is generally divided into:

Dao layer operation: generally defined in net.vschool.xxx. Dao, where XXX is the module name.

Service layer operations: generally defined in net.vschool.xxx.servie.

Web layer operations: generally defined in net.vschool.xxx.action.

The following is an example of a user module:

Net. Vschool. User

Net. Vschool. User. Dao

Net. Vschool. User action

Net. Vschool. User. The service

Rules for writing two types of names (Class)
The name of the class must be a noun. If a class name contains more than one word, the first letter of each word is capitalized and the subsequent letters are lowercase, with humps and humps. When you name a class, you must be accurate, concise, and easy to understand. Try to use full words and avoid abbreviations (except those commonly accepted)

2.1 class naming
2.1.1 general requirements
1. Choose meaningful names that quickly convey the purpose of the class.

2. According to the Java camel's hump naming method, the first letter of the class name must be capitalized. If the class name is composed of multiple words, the first letter of each word must be capitalized. Such as: StudentAnswer. Java

3. When you want to distinguish between an interface class and an implementation class, you can add "Impl" at the end of the class.

Example: interface class: userinterface. java    Interface implementation class: UserInterfaceImp

4. Recommended entity classes do not have suffixed names.

2.1.2 practical application
Layers are often applied in the application system: Dao layer (database access), Service layer (business processing), Web layer (page control action class), and the class name of each layer should be suffixed as much as possible.

1, the Dao layer

A. Interface class: it is defined in the form of JavaBean+Interface+Dao, that is, entity object +Interface+Dao.    

For example: user object interface class: UserInterfaceDao, where XXX is the module name.

B. Implementation class: defined in the form of JavaBean+Interface+Impl+Dao, that is, entity object                

+ Interface + Impl + Dao. For example: user object implementation class: UserInterfaceImplDao

2, the Service layer

A. Interface class: it is defined in the form of Xxx+Interface+Service, that is, module +Interface+Service.        

For example: user management interface class: UserMsgInterfaceServiec

B. Implementation class: defined in the form of Xxx+Interface+Impl+Service, that is, module +Interface+

Impl + Service. Such as: user management implementation class: UserMsgInterfaceImplServiec

3. Web layer (action class)

A. Implementation class: defined in the form of Xxx+Operator+Action, that is, module + operation +Action. Such as              

User module User+ Delete operation Delete+Action = UserDeleteAction

2.1 naming of variables
2.2.1 ordinary variables
2.2.2.1 general requirements
1. Choose meaningful names that quickly convey the purpose of the variable.

2. Refer to the Java camel case naming method, the first letter begins with lowercase, and the first letter of every word is capitalized (except the first word).

2.2.2.2 practical application
1. The basic structure of variable naming is typeVariableName, and the three-character prefix is used to represent the data type.

For example, define an int: intDocCount, where an int indicates the data type, followed by an ideographic English name, and the first letter of each word is capitalized.

Data type or object type

Prefix variables

note

byte

bye

1, when doing the array, add the prefix -a, such as string array: astr,

2. Variables of a custom type can take their own names and change their initial letters to lowercase.

3. Use a name that represents the meaning of the method. If employeeList: employeeList

char

CRH

float

PLT

Boolean

BLN

The Integer/int

int

short

SHT

Long/Long

LNG

A Double/Double

DBL

The string

STR


2. Use of variables:

A. Two values with different meanings are not represented by the same variable in a function.

B. It is generally not recommended to use a single letter as a variable name unless it is in a loop. I, j, k, etc., are only used as circular index variables for small loops.

C. Avoid naming state variables with flags.

D. Name the logical variable with Is, such as: blnFileIsFound. This way of naming Boolean variables in a positive form allows other developers to understand more clearly what Boolean variables mean.  

E. If you need to abbreviate variable names, you must pay attention to the consistency of abbreviations throughout the code. For example, if you use intCnt in some areas of your code and intCount in others, you add unnecessary complexity to your code. It is recommended that you avoid abbreviations in variable names.    

2.2.2 static variables
1. Choose meaningful names that quickly convey the purpose of the variable.

2. According to the Java camel's hump nomenclature, all capitals are used to write, and "_" is used to connect the words for the variables synthesized by multiple words. Such as: USER_LIST

2.3 method naming
2.3.1 general requirements
1. Choose meaningful names that quickly convey the purpose of the method.

2. Refer to the Java camel case naming method, the first letter begins with lowercase, and the first letter of every word is capitalized (except the first word).

2.3.2 practical application
1, method to express a behavior, it represents an action, preferably a verb or a phrasal verb or the first word for a verb.

2. Attribute method: start with get/set, followed by the field name, and capitalize the first letter of the field name. Such as: getUserName ()

3, data layer methods: can only start with insert (insert),delete (delete),update (update),select (search),count (statistics), other layer methods to avoid starting with the five words, so as to avoid misunderstanding.

4. Service layer method, named according to the behavior of the method, only describes the meaning of the method, not the purpose of the method named. For example, the system can add new users, users can register in the foreground, but also can be added by the administrator background, methods will be reused, so it is best not to use register, use add will be better. Avoid methods related to the web layer.

5, Web layer method is best to be close to the Web language, such as register, login, logout and other methods.

Specification for writing annotations (Javadoc)
In addition to Java's ability to use our usual way of commenting (//,), the Java language specification defines a special kind of comment, known as Javadoc comments, which can be automatically converted to online documentation, eliminating the need to document programs separately. Recommended.

Javadoc comments mainly cover scope: classes, properties, methods.

For example:    


package org.ietf.jgss;
import java.net.InetAddress;
import java.util.Arrays;
/**
 *  A holistic description of the class. 
 *
 * @author  The author 
 * @version 1.0, 05/22/07
 * @since 1.0
 */
public class ChannelBinding {
/**
 *  Comment information on the variable 
 */
private InetAddress initiator;
/**
 *  Comment information on the variable 
 */
private InetAddress acceptor;
/**
 *  Comment information on the variable 
 */
    private  byte[] appData;
   
    /**
     *  Comment information about the constructor of the class. 
     *
     * @param initAddr  Remarks to parameters. 
     * @param acceptAddr Remarks to parameters. 
     * @param appData Remarks to parameters. 
     */
    public ChannelBinding(InetAddress initAddr, InetAddress acceptAddr,
              byte[] appData) {
         initiator = initAddr;
         acceptor = acceptAddr;
         if (appData != null) {
              this.appData = new byte[appData.length];
              java.lang.System.arraycopy(appData, 0, this.appData, 0,
                   appData.length);
         }
    }
 
    /**
     *  Comment information about a specific function of the class 
     *
     * @param obj  Comment information for parameters 
     * @return  Returns the memo information for the value 
     */
    public boolean equals(Object obj) {
         if (this == obj)
              return true;
         if (! (obj instanceof ChannelBinding))
              return false;
         ChannelBinding cb = (ChannelBinding) obj;
         return Arrays.equals(appData, cb.appData);
    }
}

Other writing standards

4.1 writing specification for Jsp page names
1. All lowercase English characters and "_" composition.

2. The overall use of the module name + operation form. Such as: user_view. JSP

3. As far as possible, the Jsp page corresponds to the meaning of the action, such as UserListAction against user_list.jsp


Interface:

Name it hump. In addition to a noun, you can also name it with an adjective.

Methods:

The rule is that verbs are good for hump names, but the big difference from class names is that the first letter must be lowercase

Variables:

It is specified as a noun and is otherwise named the same way as the method. Variable names are critical, they should have a specific meaning and be easy to understand. It is generally not allowed to use a single letter as a variable name. Except for temporary variables such as counters used in loops. When using a single letter as a variable name, I, J, and K are commonly used to name plastic variables.

Constants:

The rule is to use all uppercase letters, and if the name must be represented by more than one word, separate the words with a "-". Constant requirements must be clear, can express the meaning of constant.


Related articles: