Java Naming Conventions and code styles

Source: Internet
Author: User

Java Naming Conventions and code styles
Java Naming Conventions and code styles

 

Basic naming rules package name

The package name is listed gradually based on the domain name range, which is the opposite of the domain name naming rules on the Internet.

Consists of a group of identifiers connected with ".". Generally, the first identifier is two or three lower-case letters that match the network domain name.

Example: cn.edu. xupt. JavaTest

Class, Interface Name

The class name must start with an upper-case letter and the other letters in the word must be lower-case. If the class name is composed of multiple words, the first letter of each word should be in the upper-case format, such as TestPage;

If the class name contains the abbreviation, each letter of the abbreviation should be capitalized, for example, XMLExample. Another naming technique is that the class is designed to represent objects, therefore, you should select a noun whenever possible when naming a category.

Example: People TestPage XMLExample

Method Name

The first word of the method name should start with a lower-case letter, and each word after it starts with an upper-case letter. It can be a combination of verbs, verbs, and nouns.

Set/obtain the Method of a value, which should follow the setV/getV specification.

The Method of the returned length. It should be named length.

Method for Testing a Boolean value, which should be named isV

To convert an object to a specific type of Mehod, you should name it toF.

Example: getDate (); length (); isReady (); toOracleFormat ();

Variable name

1. Names of common variables should all use lower-case letters.

2. The final static variable names should all be in uppercase and indicate the complete meaning, similar to the macro definition in C language. If a constant name is composed of multiple words, the words should be separated by underscores, for example.

Example: NUM_DAYS_IN_WEEK MAX_VALU

3. If you need to name a variable, pay attention to the consistency of the abbreviation rules in the code.

Example: context = ctx message = msg

4. At any time, constants are recommended to replace numbers and fixed strings. That is to say, the program should not contain any number except 0, 1.

5. Index variables: I, j, k, and so on are used only as cyclic index variables for small loops.

6. logical variables: Do not use flag to name state variables. Use is to name logical variables.

Example:

If (isClosed ){

Dosomeworks;

Return;

}

Set

The recommended naming method for arrays or containers is noun + s.

Example:

List persons = getPerson ();

For (Person person: persons ){

Dosomeworks;

}

Generic

It should be as concise as possible (preferably a letter) to facilitate distinction from common classes or interfaces.

The Element in the iner should be represented by E;

Key in Map is represented by K, and value is expressed by V;

Type is represented by T;

An exception is indicated by X. If you need to receive multiple types of parameters, you should use uppercase letters (for example, S) of the adjacent T to represent them in sequence. Of course, you can also use T1, T2.

Example:

Public class HashSet extends actset {

...

}

Public class HashMap Extends AbstractMap {

...

}

Public class ThreadLocal {

...

}

Public interface Functor {

T val () throws X;

}

Recommended name

1. When you want to differentiate interfaces and implementation classes, you can add "Impl" to the end of the class ".

Example: interface Container class ContainerImpl

2. It is recommended that the Exception class end with "Exception" as the class name

Example: DataNotFoundException InvalidArgumentException

3. It is best to use Abstract as the class name to start

Example: AbstractBeanDefinition AbstractBeanFactory

4. It is best to use "Test" as the end of the class name for the Test class.

Example: ContainerTest

Code style curly brackets

Curly braces are in the following format:

If (bool experssion ){

Dosomework;

}

The curly braces cannot be omitted unless they are empty.

For example:

If (I = 0 ){

Return;

}

While (true ){

}

The following statement does not appear:

Disable

If (I! = 0)

Return;

Try not

If (I! = 0)

{

Return;

}

Parentheses

There is no space for the last character before the parentheses, for example:

Person p = new Person ("Jack", 17 );

Space

1. A comma followed by a space.

Person p = new Person ("Jack", 16, "China ");

2. Semicolons in parentheses followed by Spaces

For (int I = 0; I <10; I ++ ){

Dosomework;

}

3. Leading and trailing spaces of binary operators.

Int I = a + B-c * d;

4. Space is not required for the unary operator.

For (int I = 0; I <10; I ++ ){

Dosomework;

}

5. No spaces are required before and after parentheses

Class

The class definition structure is in the order:

1) Constant

2) member variables

3) constructor

4) member functions

5) get and set methods

Leave a blank line between each part.

For example:

CATEGORY template:

Class Person {

Private final static int MAX_AGE = 100;

Private String firstname = "Jack ";

 

Public Person (){

}

Public Person (String firstname ){

This. firstname = firstname;

}

 

Public void doExercise (){

Dosomeworks;

Run ();

}

Private void run (){

Dosomeworks;

}

Public getFirstname (){

Return firstname;

}

Public setFirstname (String firstname ){

This. firstname = firstname;

}

}

Constructor

1) The constructor with null parameters appears at the top of the list.

2) constructor with call relationship is adjacent

3) The parameters should be sorted from few to many in ascending order.

Use member variables

In the class method, this is not used to reference member variables except for naming conflicts. In special cases, the get and set methods are not used to access member variables in class methods.

Method

Methods With call relationships should be placed in adjacent locations as much as possible. public and private methods can be placed across each other.

Get and set methods. All the member variables that need to be made public must comply with good javabean specifications. The get and set methods should be provided and should be automatically generated using the IDE tool whenever possible.

Javadoc comments

At the beginning of each program, Javadoc is generally used to comment on the overall description of the program and copyright information. Then, Javadoc comments can be added to each class, interface, method, and field in the main program, the first part of each comment first summarizes the functions completed by the class, interface, method, and field in one sentence. This sentence should take a separate line to highlight its generalization, after this sentence, you can follow more detailed descriptions. After descriptive paragraphs, you can also follow special paragraphs starting with the Javadoc annotation label, such as @ auther and @ version in the preceding example. These paragraphs are displayed in a specific way in the generated documents.

 

 

Related Article

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.