java Programming Specification

1 Programming Statute

1.1 named style

1, [Mandatory] Names in the code cannot begin with the scribe or dollar symbol, and cannot end the scribe or dollar symbol. Inverse example: _name / _ _name/$Object/name/name$/Object$

2, [Mandatory] Naming in the code is strictly forbidden to use the pinyin and English mix, and the Chinese method is not allowed. Description: The correct English spelling and syntax allows the reader to be easy to understand and avoid ambiguity. Note that even pure pinyin naming methods should also be avoided.
Alibaba / Taobao / Youku / Hangzhou and other international universal names, visual English.
Inverse example:
Dazhepromotion [discount] / getPingFenByname () [评] / int a variable = 3

3, [Force] Class name Use the UpperCamelcase style, must comply with the hump form

4, [Mandatory] Method, parameter name, member variable, local variables are uniformly used using the LowerCamelcase style, and must comply with the hump form.
Mogue: LocalValue / gethtpMessage () / InputUserID

5, [mandatory] constant names all uppercase, the word is separated from the word interlaced, “strive to speak full of semantics, don’t be suspected.
Master case: MAX_STOCK_COUNT Countertaine: max_count

6, [Force] Abstract class named using Abstract or Base begins; the exception class named the EXCEPTION end; the test class named the name of the class to be tested, the end of Test.

7, [Force] Brand is part of the array type, the array is defined as follows: string [] args; reverse example: Use the String Args [] to define.

8, [Force] POJO class Neble type variables, do not add IS, otherwise some framework parsing can cause sequential errors.
In the case: Defined as the basic data type Boolean isdeleted; its method is also isdeleted (), the RPC framework is in reverse parsing, “Thought” The corresponding attribute name is deleted, causing the attribute to obtain, thus throwing Exception.

9, [Force] The package is uniformly using lowercase, and there is only one natural semantic English word between the point separator. The package name is uniformly used in the form, but the class name can use a plural form if there is a complex meaning. Journal: Application Tools package is called, class name messageutils (this rule refer to Spring framework structure)

10, [Mandatory] eliminate completely irregular abbreviations to avoid ignorance. In the case of an example: AbstractClass “Abbreviation” is named ABSCLASS; condition “Abbreviation” is named CONDI, which has severely reduces the readability of the code.

11, [Recommended] In order to achieve the goal of the code self-explanation, any custom programming element is named when naming, and uses the complete word combination to express it. Journal cases named PULLCODEFROMREMOTEREPOSTORY.
Inverse example: Variable INT A; free naming

12, [Recommended] If the module, interface, class, method uses the design mode, reflects the specific mode when naming. Note: The design pattern is embodied in the name, which is conducive to the reader to quickly understand the architecture design concept.

public class OrderFactory;public class LoginProxy;public class ResourceObserver;

13, [Recommended] The methods and attributes in the interface class do not add any modified symbols (not added), keep the code’s simpleness, and effectively javadoc comments. Try not to define variables in the interface. If you must define variables, it must be related to the interface method and is the basic constant of the entire application. Journal cases: Void f (); interface basic constant representation: string company = “alibaba”; reverse example: Interface method definition: public abstract void f (); Description: JDK8 Interface Allows have default implementation, then this default The method is the default implementation of all implementations.

14, interface and implementation class have two sets of rules:

  1. [Force] For the service and DAO classes, based on SOA-based concepts, exposes
    Being must be an interface, the internal implementation class is distinguished from the suffix of IMPL and the interface. Aqueous example:
    CachaServiceImpl implements the CacheService interface.
  2. [Recommended] If it is the name of the interface, take the corresponding adjective word to interface name (pass
    Often -Able is the form of -able).
    Taste examination: AbstractTranslator implements Translatable.

15, [Reference] Enumeration Class Name Recommended to bring the ENUM suffix, enumeration member name requires full case, and the word is separated from the line.
Description: Enumeration is actually a special constant class, and the construction method is mandatory by default.
Cormented: Enumerate Name of ProcessStatusenum: Success / Unkown_reason.

16, [Reference] Name Statuitment:

  1. DAO layer method naming status
    1. Get a single object method to prefix with a GET.
    2. Get multiple objects to prefix with LIST.
    3. Get a statistical method to prefix with count.
    4. The inserted method is prefixed with Save / INSERT.
    5. The deleted method is prefixed with Remove / Delete.
    6. The modified method is prefixed with Update.
    7. Naming format DAO for each DAO file
  2. Service layer naming status
    Each service file is suffixed by Service.
  3. Controller layer naming status
    ​​Each Controller file is a suffix of Controller
  4. ENTITY layer naming status
    Each Entity file is a suffix by entity
  5. Other layers push

1.2 Constant Definition

1, [Mandatory] Does not allow any magic value (ie, undenable constant) directly appear directly in the code.
Inverse example:

String key = "Id#taobao_" + tradeId;cache.put(key, value);

2, [Force] When LONG or LONG initial assignment, use uppercase L, not lower-write L, lowercase is easy to confuse with the number 1, causing misunderstanding. Description: Long a = 2L; writing a number 21 or a LONG type 2?

3, [Recommended] Do not use a constant class to maintain all constants, categorize according to constant function, separate maintenance.
Description: The large-scale constant class is not to use the lookup function to locate the constant constant, which is not conducive to understanding and maintenance.
Cache Related constants are placed under class cacheconsts; system configuration related constants are placed under CONFIGCONSTS.

4, [Recommended] If the variable value varies only in a range, it is defined as an enumeration class with an extension attribute outside the name. The number in the following normal example is an extension information indicating the day of the week.

public Enum {     MONDAY(1),     TUESDAY(2),     WEDNESDAY(3),     THURSDAY(4),	FRIDAY(5),     SATURDAY(6),     SUNDAY(7);}

1.3 Code Specification

1, [Force] The use of braces. If it is empty in the braces, it is simply written to {}, and there is no need to wrap; if it is a non-empty code block:

  1. Do not change in front of left braces.
  2. After the left bulk is wrapped.
  3. Putting in front right parentheses.
  4. There is also a code such as ELSE after the right parentheses is not changed; it must be wrap after the right large parentheses termination.
if (a > b) {	System.out.println("a>b");} else {  	System.out.println("a<=b");}

2, [Force] There is no space between left brackets and characters; the same, there is no space between the right parentheses and characters, and the rules of the function are equally applicable to the rules of the function; see the following statement below. .
Inverse example:

if (space a == B space)

3, [Force] If / for / while / switch / do, the reserved words and parentheses must be added between the parentheses.

4, [Force] Any second, the left and right sides of the three-go operator need to add a space. Description: The operator includes assignment operators =, logical operators &&, and sub-multiplexing symbols.

5, [Force] Use 4 space indentation to prohibit the use of Tab characters. Description: If you use Tab indent, you must set 1 Tab to 4 spaces. When the IDEA sets Tabs to 4 spaces, do not check Use Tab Character; in Eclipse, you must check Insert Spaces for Tabs.
Come: (involving 1-5 points)

public static void main (string [] args) {string Say = "Hello"; // indent 4 space int flag = 0; // The argument must have a space / * Keyword IF and parentheses There must be a space; F and the left brackets in parentheses, 0 and the right brackets do not need space * / if (flag == 0) {system.out.println (SAY);} / * Left parentheses before binding Do not change the line; after the left large parentheses, the right wrap is wrapped in the right parentheses, and there is ELSE after the right braces, there is no need to change the line; after the right parentheses, the line must be completed, then the line must be wrap * / if (flag == 1) {system.out .out. Println ("world");} else {system.out.println ("ok");}}

6, [Force] There is only one space between the dual slash and the comment content. Juan: // Comment content, pay attention to there in a space between // and comment content.

7, [Force] The number of single-line characters should not exceed 120, which exceeds the need to wrap, and follow the principle of the following principles:

  1. The second line is indented with respect to the first line, starting from the third line, no longer contingent, refer to the example.
  2. The operator wraps in the following.
  3. The point symbols called by the method are wrap together below.
  4. When the method is called, multiple parameters need to be wrap, after the comma is made.
  5. Do not wrap before parentheses, see the reverse example.
StringBuffer SB = New StringBuffer (); // More than 120 characters, the wrap is retracted into 4 space, the point number, and the method name together in wrap Sb.Append ("zi"). Append ("xin"). ... append ("huang") .... Append ("Huang") ...

Inverse example:

StringBuffer SB = New StringBuffer (); // More than 120 characters, do not wrap Sb.Append ("zi") before parentheses. Append ("xin") ... append ("huang"); // Multi-parameter Method calls may exceed 120 characters, do not wrap Method before comma (Args1, Args2, Args3, ..., Argsx);

8, [Mandatory] Method parameters When defined and incorporated, multiple parameters must be added after a comma.
Cooperation: “a” in the first parameters in the following example, there must be a space behind it. Method (“a”, “b”, “c”);

9, [Force] Ide’s text file encoding is set to UTF-8;

10, [Recommended] There is no need to add a number of spaces to align the characters of a row with the corresponding position of the previous line.
Come: int A = 3; long b = 4L; float c = 5f; stringbuffer SB = new stringbuffer ();
Description: Increase this variable, if you need to align, give A, B, C to increase several spaces, which is a cumbersome thing in the case of variables.

11, [Recommended] Method of execution statement groups, variable definition statement group, between different business logic or different semants insert an air line. There is no need to insert a blank line between the same business logic and semantics. Explanation: There is no need to insert multiple blank lines.

[Continuous update. . . ]