1. [Mandatory] Names should not start or end with an underline or a dollar sign.
Counter example: _name / __name / \$Object / name_ / name\$ / Object\$
2. [Mandatory] Using Chinese, Pinyin, or Pinyin-English mixed spelling in naming is strictly prohibited. Accurate English spelling and grammar will make the code readable, understandable, and maintainable.
Positive example: alibaba / taobao / youku / Hangzhou. In these cases, Chinese proper names in Pinyin are acceptable.
3. [Mandatory] Class names should be nouns in UpperCamelCase except domain models: DO, BO, DTO, VO, etc.
Positive example: MarcoPolo / UserDO / HtmlDTO / XmlService / TcpUdpDeal / TaPromotion
Counter example: marcoPolo / UserDo / HTMLDto / XMLService / TCPUDPDeal / TAPromotion
4. [Mandatory] Method names, parameter names, member variable names, and local variable names should be written in lowerCamelCase.
Positive example: localValue / getHttpMessage() / inputUserId
5. [Mandatory] Constant variable names should be written in upper characters separated by underscores. These names should be semantically complete and clear.
Positive example: MAX_STOCK_COUNT
Counter example: MAX_COUNT
6. [Mandatory] Abstract class names must start with Abstract or Base. Exception class names must end with Exception. Test case names shall start with the class names to be tested and end with Test.
7. [Mandatory] Brackets are a part of an Array type. The definition could be: String[] args;
Counter example: String args[];
8. [Mandatory] Do not add ‘is’ as prefix while defining Boolean variable, since it may cause a serialization exception in some Java frameworks.
Counter example: boolean isSuccess; The method name will be
isSuccess()
and then RPC framework will deduce the variable name as ‘success’, resulting in a serialization error since it cannot find the correct attribute.
9. [Mandatory] A package should be named in lowercase characters. There should be only one English word after each dot. Package names are always in singular format while class names can be in plural format if necessary.
Positive example:
com.alibaba.open.util
can be used as a package name for utils;MessageUtils
can be used as a class name.
10. [Mandatory] Uncommon abbreviations should be avoided for the sake of legibility.
Counter example: AbsClass (AbstractClass); condi (Condition)
11. [Recommended] The pattern name is recommended to be included in the class name if any design pattern is used.
Positive example:
public class OrderFactory;
public class LoginProxy;
public class ResourceObserver;
Note: Including corresponding pattern names helps readers understand ideas in design patterns quickly.
12. [Recommended] Do not add any modifier, including public
, to methods in interface classes for coding simplicity. Please add valid Javadoc comments for methods. Do not define any variables in the interface except for the common constants of the application.
Positive example: method definition in the interface:
void f();
constant definition:String COMPANY = "alibaba";
Note: In JDK8 it is allowed to define a default implementation for interface methods, which is valuable for all implemented classes.
13. There are two main rules for interface and corresponding implementation class naming:
1) [Mandatory] All Service and DAO classes must be interfaces based on SOA principle. Implementation class names should end with Impl.
Positive example:
CacheServiceImpl
to implementCacheService
.
2) [Recommended] If the interface name is to indicate the ability of the interface, then its name should be an adjective.
Positive example:
AbstractTranslator
to implementTranslatable
.
14. [For Reference] An Enumeration class name should end with Enum. Its members should be spelled out in upper case words, separated by underlines.
Note: Enumeration is indeed a special constant class and all constructor methods are private by default.
Positive example: Enumeration name:
DealStatusEnum
; Member name:SUCCESS / UNKOWN_REASON
.
15. [For Reference] Naming conventions for different package layers:
A) Naming conventions for Service/DAO layer methods
1) Use get
as name prefix for a method to get a single object.
2) Use list
as name prefix for a method to get multiple objects.
3) Use count
as name prefix for a statistical method.
4) Use insert
or save
(recommended) as name prefix for a method to save data.
5) Use delete
or remove
(recommended) as name prefix for a method to remove data.
6) Use update
as name prefix for a method to update data.
B) Naming conventions for Domain models
1) Data Object: *DO, where * is the table name.
2) Data Transfer Object: *DTO, where * is a domain-related name.
3) Value Object: *VO, where * is a website name in most cases.
4) POJO generally point to DO/DTO/BO/VO but cannot be used in naming as *POJO.