[Java] Ali Java Development Manual - Partial Summary (1)

Get into the habit of writing together! This is the second day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

This is my own summary of some regulations of the Ali Java Development Manual (Huangshan Edition). There are many knowledge points in the manual that are often encountered and confused in daily development. Write this blog post for daily review, and gradually try it into work and daily development.

Access to the documentation can be downloaded via the GitHub link .

The naming style

1.1. All naming

  • All programming-related names must not_$ begin or end with an underscore or dollar sign ;
  • All programming-related naming is strictly prohibited to use the combination of Pinyin and English, and it is not allowed to use Chinese directly (according to the correct English spelling and grammar);
  • Avoid racial or abusive words in any human language in code and comments ;
  • In order to achieve the goal of self-explanation of the code, any custom programming element is named with a complete word combination to express;
  • If modules, interfaces, classes, and methods use design patterns, they should reflect the specific patterns when naming them.

1.2, class, interface

  • The class name uses the UpperCamelCase style (starting with uppercase English letters ) ;

  • Abstract class names start with Abstract or Base ;

  • Exception class names end with Exception ;

  • The test class name starts with the name of the class it is to test and ends with Test ;

  • Do not add any modifiers to the methods and properties in the interface class (do not add public), keep the code concise, and add valid Javadoc comments*;

  • 接口和实现类的命名规则:

    1. 对于Service和DAO类,内部的实现类用Impl的后缀与接口区别;

      CacheServiceImpl实现CacheService接口

    2. 如果是形容能力的接口名称,取对应的形容词为接口名(通常是-able结尾的形容词)。

      AbstractTranslator实现Translatable

  • 枚举类*名带上Enum后缀,枚举成员名称需要全大写,单词间下划线隔开。

1.3、方法、参数、成员变量、局部变量、常量、数组

  • 方法名、参数名、成员变量、局部变量使用lowerCamelCase风格(使用小写英文字母打头);
  • 常量命名应该全部大写,单词间用下划线隔开;
  • 类型与中括号紧挨相连来定义数组 Type[] arrayList
  • 在常量与变量命名时,表示类型的名词放在词尾,以提高辨识度nameList/startTime/workQueue

1.4、包名

  • 包名统一使用小写,点分隔符之间有且仅有一个自然语义的英语单词。包名统一使用单数形式,但是类名如果有复数含义,类名可以使用复数形式。

1.5、各层命名规约

  • Service/DAO层方法命名规约:

    1. 获取单个对象的方法:get为前缀
    2. 获取多个对象的方法:list为前缀,复数结尾,如listObjects
    3. 获取统计值的方法:count为前缀
    4. 插入的方法:save/insert做前缀
    5. 删除的方法:remove/delete做前缀
    6. 修改的方法:update做前缀
  • 领域模型命名规约

    1. 数据对象:xxxDO,xxx为数据表名
    2. 数据传输对象:xxxDTO,xxx为业务领域相关的名称
    3. 展示对象:xxxVO,xxx一般为网页名称
    4. POJO是DO/DTO*/BO*/VO*的统称,禁止命名成xxxPOJO

二、代码格式

2.1、大括号、小括号

  • 如果大括号内为空,简洁地写成{}即可,大括号中间无需换行和空格;

  • 如果非空代码块,则:

    public static void main(String[] args) {// 左边大括号前加空格切不换行,左大括号后换行
    			if (xxx == xx) {
    					xxxxx
    					// 右大括号前换行,右大括号后有else,else在同一行,不用换行
    			} else {
    					xxxxx
    			} // 在右大括号后直接结束,则必须换行
    }
    复制代码
  • if/for/while/switch/do等保留字与左右括号之间都必须加空格;

  • 左小括号和右边相邻字符之间不需要空格;

  • 右小括号和左边相邻字符之间不需要空格;

  • 左大括号前要加空格;

2.2、运算符

  • A space must be added to the left and right sides of any binary or ternary operator (including assignment operator =, logical operator &&, addition, subtraction, multiplication and division symbols, etc.;

2.3. Others

  • Use 4 spaces for indentation, prohibit the use of tab characters (you can set tab indentation in IDE);

  • There is one and only one space between the double slash of the comment and the content of the comment;

  • When doing a type cast, no space is required between the closing parenthesis and the cast value;

  • The number of characters in a single line is limited to no more than 120. If it exceeds, a new line is required. Follow the following principles when wrapping a line:

    • The second line is indented by 4 spaces relative to the first line, starting from the third line and no longer indented;
    • The operator wraps with the following;
    • The dot notation of a method call wraps with the following text;
    • When multiple parameters in a method call need to be wrapped, they are performed after the comma;
    • Do not wrap a line before parentheses.
  • When method parameters are defined and passed in, a space must be added after the commas of multiple parameters;

    method(arg1, arg2, arg3);

  • The total number of lines for a single method does not exceed 80 lines;

  • There is no need to add several spaces to align the equal sign of the variable assignment with the equal sign in the corresponding position on the previous line;


To be continued....

3. Summary

In the future, I will add MySQL-related and Java-related protocols one after another when I have time. Many protocols are a mess when they are not documented. For example, after reading Ali's development manual, I found that many parts of the company's framework follow this protocol but there is no document to refer to. Most of the coding styles are determined by the "big guy" who organizes or builds the framework at the beginning of the project. The main tone, some follow-up people follow the development and some have their own style, which makes many things in the project difficult to understand and understand, and redundancy and efficiency problems occur from time to time.

Recently, I was listening to the podcast program "Organizational Evolution". There are a lot of hard knowledge and dry goods. The promotion of Feishu, the father of hard broadcasting, is also very strong, which makes me feel that it is not a good company without using Feishu. But there is one thing I agree with. No matter what period of work it is, it is necessary to leave documents at work, whether it is to let others know what you have done, or what you will do, or the people behind you after you leave. How to take over your job is a good reference and a great resource for your own review. It is recommended that you actively leave documents, but not limited to the form.

Guess you like

Origin juejin.im/post/7084972383508889636