Naming style

1. [Mandatory] The naming in the code cannot start or end with an underscore or a dollar sign.

Example: /name__

2. [Mandatory] It is strictly forbidden to mix pinyin and English in the naming of the code, let alone directly use Chinese.

Example: International common names such as Alibaba/Taobao/Youku/Hangzhou can be regarded as English.

Counterexample: DaZhePromotion [discount] / getPingfenByName() [score] / int some variable = 3

Explanation: Correct English spelling and grammar make it easy for readers to understand and avoid ambiguity. Note that even pure pinyin naming should be avoided.

3. [Mandatory] Use the UpperCamelCase style for class names, except for DO, BO, DTO, VO, AO, PO, and UID.

Example: MarcoPolo/UserDO/XmlService/TcpUdpDeal/TaPromotion

Counterexample: macroPolo/UserDo/XMLService/TCPUDPDeal/TAPromotion

Explanation: the UpperCamelCase is called the big camel spelling, also known as the PascalCase, in which the first letter of the first word is capitalized, and the first letter of each subsequent word is capitalized

4. [Mandatory] Use the lowerCamelCase style for method names, parameter names, member variables, and local variables.

Example: localValue/getHttpMessage()/inputUserId

Explanation: lowerCamelCase is called “the Little Camel Spelling”. The first letter of the first word is lowercase and the first letter of each subsequent word is capitalized.

5. [Mandatory] Constant names are all capitalized, words are separated by underscores, and semantic expression is complete and clear, not too long name.

Is exemple: MAX_STOCK_COUNT

Example: MAX_COUNT

Note: Java method and variable names are limited in length to 2 to the power of 16, generally a trade-off between clarity and length.

6. [Mandatory] Abstract class names start with Abstract or Base. Exception class names end with Exception; A Test class name starts with the name of the class it is testing and ends with Test.

7. [mandatory] Type next to brace to represent array.

Example: Define an integer array, int[] arrayDemo

Counterexample: In the main argument, use String args[] to define it.

8. [mandatory] POJO class Boolean variables, do not add is prefix, otherwise part of the framework parsing will cause serialization errors.

Counter example: Define a Boolean variable private Boolean isDeleted

The attribute is defined as a basic data type Boolean isDeleted. The method name of the attribute is isDeleted(). During the reverse lookup, the RPC framework mistakenly thinks that the corresponding attribute name isDeleted, so the attribute cannot be obtained and an exception is thrown. This should have been a lesson learned from alibaba’s internal development process, and by following this specification we could reduce the occurrence of such bugs at their root.

9. [Mandatory] All package names are in lower case, and there is only one natural semantic English word between dot delimiters. The package name is singular, but the class name can be plural.

Example: Application utility class package named com.alibaba.ai.util and class named MessageUtils (refer to Spring framework for this rule)

10. [Mandatory] Put an end to completely non-standard abbreviations and avoid looking at the text without knowing the meaning.

Counterexample: AbstractClass “abbreviated” to AbsClass; Condition is “abbreviated” to condi, and such arbitrary abbreviations seriously reduce the legibility of the code.

Note: This point needs to be very careful, we may abbreviate the name of the variable because it is too long in the process of coding, most of the time they can understand, but reduce the readability of the code. Don’t be afraid of a long name, a complete and clear message is better practice.

11. In order to achieve the goal of self-interpretation of code, any defined programming element should be named using as complete a word combination as possible.

Is: in the JDK, express atomic updates class called: AtomicReferenceFieldUpdater

Counterexample: Arbitrary naming of the variable int a.

Note: The name should be able to explain the meaning of the element to improve the readability of the code. Do not use temporary naming methods, such as list, temp, etc. Brush algorithm brush more prone to this situation.

12. [Recommendation] If modules, interfaces, classes, and methods use design patterns, they should be named to reflect the patterns.

Is:

public class OrderFactory;

public class LoginProxy;

public class ResourceObserver;

Note: The design pattern is reflected in the name, which is helpful for readers to quickly understand the architectural design concept.

13. Do not add any modifiers to the methods and properties of the interface class (nor public), keep the code simple, and add valid Javadoc comments. Try not to define variables in the interface; if you do define variables, they must be related to interface methods and are the base constants for the entire application.

Example: Interface method signature void commit(); String COMPANY = “alibaba”;

Counterexample: The interface method defines public abstract void f();

Note: The interface Chinese method is implicitly specified as public abstract, and only public abstract. Other modifiers such as protected, private, will report an error. After JDK8, interfaces are allowed to be decorated with default methods, which need to have a method body and be a valuable default implementation for all implementation classes. Jdk9 allows you to define private methods.

Variables in the interface are implicitly specified as public static final, and only public. Other modifiers such as protected, private, and because they are final, must be explicitly initialized.

Jdk7-9 Interface changes

14. There are two sets of rules for naming interfaces and implementation classes:

1) [Mandatory] For Service and DAO classes, based on the SOA concept, the exposed Service must be an interface, and the internal implementation class is distinguished from the interface by the Impl suffix.

Example: CacheServiceImpl Implements the CacheService interface.

2) 【 recommended 】 If the interface name describes the capability, use the corresponding adjective as the interface name (usually in the form of – able).

Example: AbstractTranslator implements the Translatable interface.

14. It is recommended that enumeration class names be suffixed with Enum. Enumerator names must be in all uppercase letters and separated by underscores (_).

Example: Enumerate the member name of ProcessStatusEnum: SUCCESS/UNKNOWN_REASON.

Note: Enumerations are simply special classes in which the field members are constant and the constructor is forced to be private by default.

16. [Reference] Naming conventions for each layer:

A) Service/DAO layer method naming convention

1) Methods that get individual objects are prefixed with get.

2) Get multiple object methods prefixed with list, plural end with: listObjects.

3) Methods to obtain statistics are prefixed with count.

4) Insert methods prefixed with save/insert.

5) Delete methods with remove/delete prefix.

6) Update methods are prefixed with update.

B) Naming conventions for domain models

1) Data object: xxxDO, XXX is the name of the data table.

2) Data transfer object: xxxDTO, XXX is the name related to the business field.

3) Display object: xxxVO, XXX is generally the name of the web page.

4) POJO is a general name of DO/DTO/BO/VO. It is forbidden to name it xxxPOJO.

conclusion

This part is mainly about the development of some naming norms and styles, you can see as far as possible to achieve a unified style, while improving the readability and self-interpretation of the code, hoping to do look at the text to know the meaning, see the name to know what type this class is, what this method can do, what this variable represents.

The coding specification is subjective, some people like line breaks, some people like Spaces, but in team development, it needs to be hard and uniform to facilitate collaboration between team members.

Welcome to follow the wechat public number [do not know goodbye to ask me], thank you ~