Skip to content

Naming Conventions

Rules

1. Names should not start or end with an underline or a dollar sign. [Mandatory]

❌ _name / __name / \$Object / name_ / name\$ / Object\$

2. 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. [Mandatory]

3. Class names should be nouns in UpperCamelCase except domain models: DO, BO, DTO, VO, etc. [Mandatory]

✔ MarcoPolo / UserDO / HtmlDTO / XmlService / TcpUdpDeal

❌ marcoPolo / UserDo / HTMLDto / XMLService / TCPUDPDeal

4. Method names, parameter names, member variable names, and local variable names should be written in lowerCamelCase. [Mandatory]

✔ localValue / getHttpMessage() / inputUserId

5. Constant variable names should be written in upper characters separated by underscores. These names should be semantically complete and clear. [Mandatory]

✔ MAX_STOCK_COUNT

❌ MAX_COUNT

6. 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. [Mandatory]

7. Brackets are a part of an Array type. The definition could be: String[] args; [Mandatory]

❌ String args[];

8. Do not add is as prefix while defining Boolean variable, since it may cause a serialization exception in some Java frameworks. [Mandatory]

❌ 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. 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. [Mandatory]

✔ com.webex.mct.util can be used as a package name for utils; MessageUtils can be used as a class name.

10. Uncommon abbreviations should be avoided for the sake of legibility. [Mandatory]

❌ AbsClass (AbstractClass); condi (Condition)

✔ Some abbreviations could be used such as MCT, MAV, UAS and GSS, etc.

11. There are two main rules for interface and corresponding implementation class naming:

  1. All Service and DAO classes must be interfaces based on SOA principle. Implementation class names should end with Impl. [Mandatory]

    ✔ CacheServiceImpl to implement CacheService.

  2. If the interface name is to indicate the ability of the interface, then its name should be an adjective. [Recommended]

    ✔ AbstractTranslator to implement Translatable.

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

💡 Including corresponding pattern names helps readers understand ideas in design patterns quickly.

✔ method definition in the interface: void f();
constant definition: String COMPANY = "cisco";

💡 In JDK8 it is allowed to define a default implementation for interface methods, which is valuable for all implemented classes.

💡 Enumeration is indeed a special constant class and all constructor methods are private by default.

✔ Enumeration name: DealStatusEnum; Member name: SUCCESS / UNKOWN_REASON.

15. Naming conventions for different package layers:

- Use get as name prefix for a method to get a single object. - Use list as name prefix for a method to get multiple objects. - Use count as name prefix for a statistical method. - Use insert or save as name prefix for a method to save data. - Use delete or remove as name prefix for a method to remove data. - Use update as name prefix for a method to update data.

- Data Object: *DO, where * is the table name. - Data Transfer Object: *DTO, where * is a domain-related name. - Value Object: *VO, where * is a website name in most cases. - POJO generally point to DO/DTO/BO/VO but cannot be used in naming as *POJO.

Reference

- Alibaba Java Coding Guidelines - Naming Conventions