Knowledge changes Destiny (i): Java code specification

Source: Internet
Author: User

(i) Naming conventions

1. The names in the code cannot start with an underscore or dollar sign, or end with an underscore or dollar sign.

Counter example: _name/__name/$Object/name_/name$/object$

2. The naming in the code is strictly forbidden to use Pinyin in combination with English, and it is not allowed to use Chinese in a direct manner. Description: Correct English spelling and grammar can make readers easy to understand and avoid ambiguity. Note that even pinyin naming methods should be avoided.

Counter example: dazhepromotion [discount]/Getpingfenbyname () [score]/int a variable = 3

Positive example: Alibaba/taobao/youku/hangzhou and other international common name, visual and English.

3. Abstract class naming begins with abstract or Base; The exception class name uses the Exception end; the test class name starts with the name of the class it is testing, ending with test.

4. The brackets are part of the array type, and the array is defined as follows: string[] args;

Counter example: Do not use String args[] to define the method.

5. Package names are unified using lowercase, and there is only one natural semantic English word between the dot separators. The package name is used in singular form, but if the class name has a plural meaning, the class name can use the plural form.

Example: Application tool class package named Com.alibaba.open.util, class name Messageutils (this rule references spring's frame structure)

6. If design mode is used, it is recommended to show the specific pattern in the class name. Description: The design pattern is embodied in the name, which facilitates the reader to quickly understand the architecture design idea.

Positive example: public class orderfactory;

public class Loginproxy;

public class Resourceobserver;

7. Methods and properties in the interface class do not add any modifier symbols (public also do not add), keep the code concise, and add a valid Javadoc comment. Try not to define variables in the interface, if you must define a variable, it is definitely related to the interface method and is the underlying constant for the entire application.

Example: Interface method signature: void F (); Interface base constant means: String company = "Alibaba";

Counter Example: Interface method definition: Public abstract void f ();

8. Enumeration class names are suggested with the enum suffix, which requires all uppercase, with underscores between the words. Description: Enumerations are really special constant classes, and construction methods are forced by default to be private.

Positive Example: enumeration name: Dealstatusenum, member name: Success/unkown_reason.

9. Naming conventions for each layer:

A) Service/dao layer method naming protocol

1) The method to get a single object is prefixed with get.

2) The method to get multiple objects is prefixed with the list.

3) The method that gets the statistic value is prefixed with count.

4) Insert the method prefix with save (recommended) or insert.

5) Remove the method prefix with remove (recommended) or delete.

6) The modified method is prefixed with update.

B) domain model naming conventions

1) Data object: Xxxdo,xxx is the name of the data table.

2) Data Transfer object: Xxxdto,xxx the name that is relevant for the business domain.

3) Display object: Xxxvo,xxx is generally the name of the page.

4) POJO is a general term for DO/DTO/BO/VO, and is forbidden to be named Xxxpojo.

(ii) Constant definition

1,long or long initial assignment, must use uppercase L, not lowercase l, lowercase is easily confused with the number 1, causing misunderstanding.

Description: Long a = 2l; is the number 21 or the Long type 2?

2, the constant reuse level has five layers: across application shared constants, in-app shared constants, in-sub-engineering shared constants, in-package shared constants, in-Class shared constants.

1) shared constants across applications: Placed in a two-party library, usually under the constant directory in Client.jar.

2) in-app shared constants: placed in the constant directory in the modules of a party library.

Inverse example: the easy-to-understand variable is also uniformly defined as an in-app shared constant, and the two-bit siege division defines a variable that represents "Yes" in two classes: Class A: public static final String Yes = "yes"; Class B: public static final String YES = "Y"; A.yes.equals (B.yes), expected to be true, but actually returned to false, resulting in a problem on the line

3) Sub-project internal shared constants: that is, in the current sub-project constant directory.

4) in-Package shared constants: That is, under the current package under a separate constant directory.

5) in-Class shared constants: Directly within the class private static final definition.

Knowledge changes Destiny (i): Java code 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.