Java Coding Specification

Source: Internet
Author: User

Naming conventions

Package Name: lowercase, naming meaningful

Class name, Interface: Capital letter (CAML style)

Method: The first letter is lowercase, the following words are capitalized. Use a meaningful word or word shorthand

Properties, variables: Same method name

Constants: All uppercase, using _ Split

Formal parameters: To be consistent with variable names

Arrays: square brackets behind variable types

Documentation notes:

File Comments:

/********************************************************************

* File name: Test.java

* Copyrights: Copyright (C) XXX. All rights Reserved.

* Modified Person: XXX

* Revision Date: 2016-8-22

* Modify Content: Add ToString Method

*******************************************************************/

Class Comments:

/**

* This class is the processing class for logs (functional responsibilities)

* @author

* @since JDK1.8

* @history 2016-8-16 XX New

*/

Method notes;

/**

* Function Description

* @param Context application contexts

* @param Bean

* @return Databean

* @throws Exception If an error occurs during conversion, Exception is thrown

*/

Attribute Comment:

/** the meaning of the Description field */

Special NOTES:

Todo

A place that has not been completed but must be completed

Fixme

Where the bug must be modified

If a method is an implementation of an interface, then the detailed method document comment should be written in the method of the interface class, and the implementing class method should guide the past through the @see.

Code optimization:

1. Avoiding the "God class"

2. Try to separate some logic implementations, especially reusable ones, into different methods

3. Strictly according to JavaBean Unified Way to write property access getter, setter method, otherwise in the Java reflection mechanism implementation, there will be problems

4. Method invocation speed comparison, from fast to slow arrangement: Static < Final < Instance method < interface method < synchronized.

5.finalize method to avoid use as much as possible

6. Use static variables whenever possible. If a variable in a class does not change with his instance, it can be defined as a static variable, so that all of his instances share the variable

7. Do not hardcode specific values in your code, define them as static constants, and then reference them in your code

8. Use interface declaration properties and variables as much as possible:

such as:list<string> list=new arraylist<~> ();

Map map = new HashMap ();

9. Use local variables in the method, be sure to determine the uniqueness of the variable assignment. Otherwise, the code will be difficult to maintain, ambiguous, and a hotbed of hidden bugs.

Code style:

Each row declares a variable

int A;

int b;

Each line contains a maximum of one statement

argv++; Correct

argc++; Correct

argv++; argc--; avoid!

about how strings are handled:

1. Avoid using "+" in the string, try to use StringBuffer to connect;

2. When you use the Equals and Equalsignorecase methods to determine whether two string type operands are equal, if one of the two operands is a string constant, it should be placed in the point operator "." To the left, place the variable on the right side of the dot operator. The advantage of this is that programmers can use the Equals and Equalsignorecase methods without having to first determine if the variable operand is null

Switch:

In any case, the default statement must be written at the end.

About Exceptions:

1. Avoid the logic of the application Try/catch, if you can use if, while and other logical statements to handle, then as far as possible without try/catch statements

2. Reuse exceptions, and whenever possible, reuse an existing exception object when it is necessary to handle the exception. It takes most of the time to generate an exception object in the handling of the exception.

Debugging for log information:

Do not print debug information or exception directly using System.out or System.err or exception's Printstacktrace () method

Resource release:

Manual use of resources to be released in a timely manner, such as file resources, database resources and so on. (Although this class may implement the Atupcloseable interface)

Precautions for the use of java.util packages:

Avoid using the Java.util.Vector and java.util.Hashtable in the Java.util package, because these two classes are synchronized, which is much slower than the container classes that are not synchronized, and use Java.uti if you are single-threaded L.arraylist (Interfaces are: Java.util.List) and Java.util.HashMap (interface is JAVA.UTIL.MAP).

Name of the tool class:

Tool Description + Util

Unified management naming for module-specific constants

Module name + Constants

Each layer of code Division:

Page:

Show data

? Perform page data validation

? Do not handle application-related business logic

? Do not perform transaction management

? Complex logic, please hand it to the tool class to handle

Web tier:

? Manage requests made by the user interface layer and respond accordingly

? Get the appropriate user input data from the page

? Provides data to the business layer, delegating calls to business logic and other top-level processing

? Uniform handling of exceptions, including those thrown by the persistence layer and the business layer

? Provide a data model for page display

? User identity and Operation permission validation

? Do not perform transaction management

Biz Business Layer:

? Business logic and business validation for processing applications

Unified Management Services

Do not communicate directly with the database

? Do not communicate directly with JSP, session, etc.

Java Coding Specification

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.