Java Development Naming conventions

Source: Internet
Author: User

The purpose of defining the specification is to unify the code style of the project so that the program is readable.

Good habits also contribute to our growth. Today, together with you, Java in the development process of the language specification.

Precautions before use:

1, due to the Java object-oriented programming characteristics, in naming should try to select the noun

2. Hump nomenclature (Camel-case): When a variable name or function name is a unique identifying word that is composed of one or more words, the first letter starts with lowercase letters, with the first letter of each word capitalized (with the exception of the word).

such as: MyFirstName

Written specification (package) with a pack of names

It is recommended to prefix the package name with the top-level domain name of the company or organization in order to guarantee the uniqueness of the package name used within each company/organization. The package name is all lowercase and has a practical distinction.

1.1 General requirements

1, the choice of meaningful names, can quickly convey the purpose of the class.

2. All packages must be named with lowercase English letters.

1.2 Practical application

Hierarchy is often applied in the application system, DAO layer (Database access), service layer (business processing), Web layer (page control action Class).

1, the package name of the first few for fixed name, if the site, the use of the site's domain name of the anti-write, if the domain name has not been determined, using the company fixed several names. such as: Net.vschool

2. The next word in the package name is the name of the module. For example: User module, package name Net.vschool.user

3, about the module access operation, the use of layered form, generally divided into:

DAO layer operations: 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.service

Writing specification for two-class names (class)

The class name must use a noun, if a class name contains more than one word, then the first letter of each word uppercase, followed by lowercase letters, undulating hump-like, called the camel name. When naming a class name, you must ensure that it is accurate, concise, and easy to understand. Use full words as much as possible and avoid abbreviations (except those that are generally accepted)

2.1 Class of naming 2.1.1 General requirements

1, the choice of meaningful names, can quickly convey the purpose of the class.

2, referring to the Java Hump naming method, the first letter of the class name must be in uppercase form, if the class name is a combination of multiple words, then the first letter of each word must use uppercase. such as: Studentanswer.java

3, when you want to distinguish between the interface class and implementation class, you can add "Impl" after the class.

such as: interface class: Userinterface.java interface implementation class: Userinterfaceimp

4, the recommended entity class does not have a suffix name.

2.1.2 Practical Application

Hierarchy is often applied in the application system, the DAO layer (database access), service layer (business processing), Web layer (page Control action Class), each layer of the class name as far as possible with the layer suffix.

1. DAO layer

A, interface class: In the form of Javabean+interface+dao to define, that is, the entity object +interface+dao. Such as: User Object interface class: Userinterfacedao, where xxx is the module name.

b, the implementation of the class: the form of Javabean+interface+impl+dao to define, that is, the entity object +interface+impl+dao. For example: User Object implementation class: Userinterfaceimpldao

2. Service Layer

A, the interface class: Uses the form of xxx+interface+service to define, namely, the module +interface+service. Such as: User Management interface class: Usermsginterfaceserviec

b, the implementation of the class: the form of xxx+interface+impl+service to define, that is, the module +interface+

Impl+service. Such as: User Management Implementation class: Usermsginterfaceimplserviec

3. Web layer (Action Class)

A, the realization class: Uses the county xxx+operator+action the form to define, namely, the module + operation +action. such as user module user+ delete operation delete+action = Userdeleteaction

2.1 Names of variables 2.2.1 Common variables 2.2.2.1 General requirements

1, choose meaningful name, can quickly convey the purpose of the variable.

2, referring to the Java Hump naming method, the first letter starts with lowercase, each word capitalized (except for the first word).

2.2.2.2 Practical Application

1, the variable name uses the basic structure as Typevariablename, uses the 3 character prefix to represent the data type.

For example, define an shaping variable: intdoccount, where int indicates the data type, followed by the English name of the table, with the first letter of each word capitalized.

Data type or object type

Variable prefix

Note

Byte

Bye

1, do array time, and then prefix-A, such as String array: Astr,

2. A variable of a custom type can use its own name and change the initial letter to lowercase.

3, the use of name to be able to represent the meaning of the method. If Employee list: EmployeeList

Char

Chr

Float

Flt

Boolean

bln

Integer/int

Int

Short

Sht

Long/long

Lng

Double/double

Dbl

String

Str

2, the use of variable skills:

A, in a function does not use the same variable to represent the meaning of the two values before and after.

B, unless it is in the loop, it is generally not recommended to use a single letter as the variable name, I, J, K, etc. only as a circular index variable of the small loop.

C, avoid naming state variables with flag.

D, use is to name the logical variables, such as: Blnfileisfound.  This gives the Boolean variable a positive form of naming, allowing other developers to understand more clearly what Boolean variables represent.

e, if you need to abbreviate the variable name, be sure to pay attention to the consistency of the abbreviation rules throughout the code. For example, if you use intcnt in some areas of your code, and you use intcount in other areas, you add unnecessary complexity to your code.   It is recommended to avoid abbreviations in variable names.

2.2.2 Static variables

1, choose meaningful name, can quickly convey the purpose of the variable.

2, referring to the Java Hump naming method, the use of all uppercase form to write, for the use of multi-word synthesis of the variable using "_" to connect the words. such as: User_list

2.3 Naming 2.3.1 General requirements for methods

1, choose meaningful name, can quickly convey the purpose of the method.

2, referring to the Java Hump naming method, the first letter starts with lowercase, each word capitalized (except for the first word).

2.3.2 Practical Application

1, the method represents an action, it represents a movement, preferably a verb or a verb phrase or the first word is a verb.

2, Attribute method: Start with Get/set, followed by field name, field name first letter uppercase. such as: GetUserName ()

3, the data layer method: Only insert (insert), delete (delete), update (update), select (Find), count (statistics) at the beginning, the other layer method to avoid the beginning of this 5 words, so as not to cause misunderstanding.

4, the service layer method, according to the behavior of the method named, only describes the meaning of the method, but not the purpose of the method named. For example, the system to add new users, the user can register the front desk, can also be added to the administrator background, the method will be reused, so it is best not to use the register, using add will be better written. Avoid using methods that are related to the Web tier.

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

Written specification for three annotations (Javadoc)

In addition to our common notation (//,/*/), Java Language Specification defines a special kind of annotation, which is what we call a Javadoc comment, which begins with/**, and ends with */, Javadoc annotations can be automatically converted to an online document. Eliminates the hassle of writing program documents separately. Recommended use.

Javadoc annotations mainly cover the scope: classes, properties, methods.

For example:

Package ORG.IETF.JGSS;

Import java.net.InetAddress;

Import Java.util.Arrays;

/**

* the entirety of the class is described.

*

* @author Author

* @version 1.0, 05/22/07

* @since 1.0

*/

public class Channelbinding {

/**

* Note Information for this variable

*/

Private inetaddress Initiator;

/**

* Note Information for this variable

*/

Private InetAddress acceptor;

/**

* Note Information for this variable

*/

Private byte[] AppData;

/**

* Comment Information for the constructor of the class.

*

* @param initaddr comments on the parameters.

* @param acceptaddr comments on the parameters.

* @param AppData comments on the 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);

}

}

/**

* Note information about a specific function of the class

*

* @param Note Information for the obj parameter

* @return The comment information for the return 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);

}

}

Four other writing Specifications 4.1 JSP page name writing specification

1. All use lowercase English characters and "_ " composition.

2. The whole takes the form of module name + operation. such as: user_view.jsp

3. The JSP page corresponds to the action's meaning as much as possible, such as the userlistaction counterpart user_list.jsp

Interface:

Use hump-style naming. In addition to nouns, you can also use adjectives to name (reflect their functional characteristics)

Method:

The rule is named after the verb, which is suitable for the hump type, but the biggest difference from the class name is that the first letter must be lowercase

Variable:

Rules are nouns, and others are named in the same way as "method". Variable names are critical, should be meaningful and easy to understand, and generally do not allow the use of a single letter for variable names. Except for some temporary variables, such as counters used in loops. When you use a single letter to make a variable name, I, J, and K are used to name the shaping variable.

Constant:

All uppercase letters indicate that if the name must be expressed in more than one word, the words are separated by "-". Constant requirements must be meaningful and can express the meaning of constants.

Java Development Naming conventions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.