Java coding Specification

Source: Internet
Author: User
Tags coding standards

1.1 objectives and requirements

Why coding standards?

Coding standards are especially important for programmers for the following reasons:

-80% of the cost for a software life cycle is maintenance.

-Almost no software is maintained by the original developer throughout its lifecycle.

-Coding specifications can improve the readability of software and allow programmers to understand new code as soon as possible.

-If you release the source code as a product, you need to ensure that it is well packaged and clear, just like any other product you have built.

In order to implement the specification, each software developer must comply with the encoding specification.

This is a standard for forcible execution, not a guide. When our standards do not adapt, we can discuss and modify standards. However, existing standards must be implemented.

 

1.2 check list

 

File Type

Serial number

Normative points

References

JAVA files

1

Whether the JAVA file has the required copyright and version information ("head" for short ").

1.3

2

After each modification to the JAVA file, do you want to add the modification information to the HISTORY of "head.

1.3

3

Whether the "class", "class variable", and "Method" in the JAVA file comply with the standard annotations.

1.4

4

Whether there are a certain number of annotations in the "method.

1.4

5

Whether the elements in the JAVA file comply with the JAVA Naming rules.

1.5

6

Whether the entire writing format and the sequence of elements in a JAVA file are standardized.

1.6

1.3 copyright and version information of JAVA files ("Header" for short ")

The Copyright and version information must start with the java file. Other information that does not need to appear in javadoc can also be included here.

All header files can be automatically generated. For more information, see <Introduction to automatic release of new classes created by eclipse.rar>

For example:

/*

* Author: wufengt

* Created Date: 2008-12-1 *

 

Date Format: YYYYMMDD

* History:

*------------------------------------------------------------------------------

* Date | Author | Change Description

* 20051212 | wuf | optimized encoding format

* 20051215 | wuf | optimizes the encoding format again.

*/

1.4 JAVA file annotation specifications

1. Class Annotation

Class annotations are generally used to explain the class. They mainly include the function descriptions, precautions, and usage examples of some important functions of the class.

/**

* Comment content

*/

Public class CounterSet extends Observable implements Cloneable {

...

}

2Annotation of member variables

Public member variables must generate a document (JavaDoc ). The member variables defined by proceted, private, and package can be annotated using general annotations or JavaDoc. All member variables must be annotated.

 

// Operation Status flag: 0. New, 1. Modify, 2. View

Protected long modifyStatus = 0;

 

/**

* Number of entries per page

*/

Public Int pageSize = 0;

 

3. Method Annotation

All methods must be annotated using JavaDoc, and all methods must be annotated.

/**

*Save a session Object

* @ Param keyVariable name

* @ Param valInput the corresponding object

*/

Public void setSessionValue (String key, Object val ){

...

}

 

4. Method internal comments

A complex method must contain a certain number of comments. You can use a single line or multiple lines of comments. The content of the comment must be clear, clear, and accurate, so as to prevent the ambiguity of the comment. Keep comments adjacent to the code described by them, that is, the proximity principle of comments. Comments to codes should be placed at the top of the code and should not be placed below; comments to each variable in the structure should be placed on the right side of the variable; comments to different fields in the same structure should be aligned. Two annotation methods are generally used:

// Comment a row

/* ...... */Annotate several rows

 

5. Javadoc annotation label syntax

@ Author the description of the class indicates the author who developed the class module.

@ Version the description of the class indicates the version of the class module.

@ See references to classes, attributes, and methods, that is, related topics

@ Param: Description of a parameter in the other method

@ Return description of the method description of the return value of the Method

// @ Exception description of methods to describe possible exceptions thrown by methods

 

1.5 naming rules for JAVA Elements

General naming conventions:

1) try to use the complete English Descriptor

2) use terminology applicable to relevant fields

3) use a mix of upper and lower case to make the name readable

4) Try to use less abbreviations, but if you use them, use them wisely and unify them throughout the project.

5) Avoid using long names (preferably less than 15 letters)

6) Avoid using similar names, or simply name different cases.

7) Avoid using underscores (except static constants)

 

Naming rules:

 

Element name

Naming rules

Example

Package)

A complete English descriptor should be composed of lowercase letters. For global packages, reverse your Internet domain name and connect the package name.

Com. srt. moa. action

Class)

The complete English descriptor is used, and the first letter of all words is capitalized.

Customer,

SavingsAccount

 

Interface)

It is encapsulated using the complete English descriptor description interface, and the first letter of all words is capitalized. Traditionally, the name is followed by the suffix "able", "visible", or "er", but this is not necessary. (No Interface suffix is required)

Contactable,

Prompter

 

Exception)

E is usually used to indicate exceptions.

E

Class variable

The fields are described in full English. The first letter is lowercase and the first letter of any intermediate word is uppercase.

FirstName,

LastName

 

Local variable

Naming rules for similar variables

 

Obtain and set member functions

Get is prefixed with get before the field name to be accessed by the member function. To obtain all the Boolean Functions of the member function, the word is prefixed with "is.

Set the prefix set before the field name accessed by the member function. (Automatically generated using eclipse)

GetFirstName (),

GetLastName ()

IsPersistent (),

IsString ()

SetFirstName (),

SetLastName (),

SetWarpSpeed ()

 

Common member functions

Ordinary member functions use a complete English description to describe member functions. The first word uses a vivid verb as much as possible, and the first letter is in lowercase.

OpenFile (),

AddAccount ()

 

Static constant field (static final)

All words are separated by underscores.

MIN_BALANCE,

DEFAULT_DATE

 

Cyclic counter

Generally, I, j, k, or counter are acceptable.

I, j, k, counter

 

Array

The array should always be named in the following way: byte [] buffer;

Instead:

Byte buffer [];

Byte [] buffer

1.6 JAVA typographical specifications and sequence of Elements

1. Typographical specifications

2. Add spaces between keywords and operators.

2. Empty lines are added between relatively independent blocks.

2. Long statements and expressions should be written into multiple lines of books.

2. The new lines should be indented to make the layout neat and the statement readable.

2. A long expression divides a new line at the lower-priority operator, which is placed at the beginning of the new line.

2. If a long expression or statement exists in statements such as loop and judgment, appropriate division is required.

2. If the parameters in a function or process are long, appropriate division is required.

2. You are not allowed to write multiple phrase sentences in one row, that is, only one statement can be written in one row.

2. The code in the statements such as the start of a function or process, the definition of the structure, and the cycle and judgment should adopt the indent style.

2. the JAVA language uses braces '{' and '}' to define a block. When writing a block, '{' and
'}' Each row must have an exclusive row in the same column and be left aligned with the statements that reference them. At the beginning of the function body, the definition of the class, the definition of the structure, the definition of enumeration, and the programs in the if, for, do, while, switch, and case statements must adopt the above indent method.

If (I> 0) {I ++}; // error, {And} are in the same row

 

If (I> 0)

{

I ++

}; // Correct, {separate row

 

If (I> 0 ){

I ++

}; // Correct, {separate row

 

 

 

2. Element order

/* Copyright version information */

 

Package/Imports

 

Class Definition (including extends and implements public class CounterSet in different rows)

Extends Observable

Implements Cloneable

{

Class Fields)

 

Constructor

 

Clone Method

 

Class Method

 

Access Method (get/set Method)

 

ToString Method

 

Main Method

}

Attachment:

Sun (Java coding Specification): http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html

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.