Java programming style and naming rules, java programming style specifications

Source: Internet
Author: User

Java programming style and naming rules, java programming style specifications

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.

A group uses "." The identifier of the connection. Generally, the first identifier is two or three lower-case letters that match the network domain name.

People TestPage XMLExample

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 in the written word 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.

People TestPage XMLExample

Method Name

The first word of the method name should start with a lower-case letter, and the subsequent words should start 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.

GetDate (); length (); isReady (); toOracleFormat ();

Variable name

1. The names of common variables should be in the upper case of the first letter and the other letters in the upper case.

2. The final static variable names should all be in uppercase and indicate the complete meaning. If a constant name is composed of multiple words, the words should be separated by underscores, for 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.

Context = ctx message = msg

4. You can create more unified variables by placing a quantizer at the end.

First (First in a group of variables) Last (Last in a group of variables) Next (Next in a group of variables) Prev (previous in a group of variables) cur (the current variable in a group of variables)

5. 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.

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

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

If (isClosed) {dosomeworks; return ;}

Array

Always use the following method to define an array:

Int [] arr = new int [10];

C language is prohibited:

Disable int arr [] = new int [10];

Set

The recommended naming method for arrays or containers is noun + s, for 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 class or interface

The Element in the iner should be represented by E; the key in the Map is represented by K; the value is represented by V; the Type is represented by T; and the exception is represented by X.

If you want 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.

Public class HashSet extends actset {...} Public class HashMap <k, v = ""> extends AbstractMap <k, v = ""> {...} Public class ThreadLocal {...} Public interface Functor <t, throwable = "" extends = "" x = ""> {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 ".

Interface Container class ContainerImpl

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

DataNotFoundException InvalidArgumentException

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

AbstractBeanDefinition AbstractBeanFactory

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

ContainerTest

5. Abbreviation and abbreviation (not recommended)

Cp stands for colorPoint buf stands for buffer off stands for offset len stands for length

Unless it is in a loop, it is generally not recommended to use a single letter as the variable name, but there are also exceptions, that is, a single letter.

B stands for byte c stands for char d stands for double E generation table Exception f stands for float I, j, k stands for integer l stands for long o stands for Object s stands for String v stands for some types of specific values

Code style

Curly brackets

Curly braces are in the following format:

If (bool experssion) {dosomework ;}

Unless empty, curly brackets cannot be omitted in any case and must be wrapped in a line break, for example:

If (I = 0) {return;} while (true ){}

The following statement does not appear:

Disable if (I! = 0) return; disable if (I! = 0) {return ;}

Brackets

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

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

Space

A comma followed by a space.

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

2. Leading and trailing spaces of binary operators.

Int I = a + B-c * d;

3. Space is not required for the unary operator. The for statement has a space after the semicolon.

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

4. No spaces 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 ;}}

2. 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.

3. Use member variables

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

4. 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.

5. For the get and set methods, all the member variables to be made public must comply with the good javabean specification. 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, A more detailed description section can be followed after this sentence. After descriptive paragraphs, you can also follow special paragraphs starting with the Javadoc annotation label, such as @ auther and @ version in the above example. These paragraphs will be displayed in a specific way in the generated document.

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.