Summary of Ali Java Specification Manual


This article summarizes some common coding attention problems. Each specification comes from each module, so the serial number is a bit messy. Some simple and rarely used specifications are not included. The specific specifications can be found at the end of the article. Ali java specification.pdf, download and read in detail

OOP protocol

7. [Mandatory] All values ​​of the same type of wrapper objects are compared using the equals method. Description: For the assignment of Integer var = ? in the range of -128 to 127, the Integer object is generated in IntegerCache.cache, and the existing object will be reused. The Integer value in this range can be directly judged by ==, but this range All other data will be generated on the heap and will not reuse existing objects. This is a big pit. It is recommended to use the equals method for judgment.

8. The usage standards for basic data types and wrapper data types are as follows:
1) [Mandatory] All POJO class attributes must use wrapper data types.
2) [Mandatory] Return values ​​and parameters of RPC methods must use wrapper data types.
3) [Recommended] All local variables use primitive data types.
Note: There is no initial value for POJO class attributes to remind users that they must explicitly assign values ​​when they need to be used. Any NPE problems or warehousing inspections are guaranteed by users. Positive example: The query result of the database may be null, because of automatic unboxing, receiving with basic data types has NPE risk. Counter example: For example, to display the ups and downs of the total turnover, that is, positive or negative x%, x is the basic data type, the RPC service called, when the call is unsuccessful, the default value is returned, and the page displays 0%, which is unreasonable. It should appear as a dash. Therefore, the null value of the wrapped data type can represent additional information, such as: remote call failure, abnormal exit.

11. [Mandatory] It is forbidden to add any business logic in the construction method. If there is initialization logic, please put it in the init method.

12. 【强制】POJO类必须写toString方法。使用IDE的中工具:source> generate toString时,如果继承了另一个POJO类,注意在前面加一下super.toString。 说明:在方法执行抛出异常时,可以直接调用POJO的toString()方法打印其属性值,便于排查问题。

20. 【推荐】类成员与方法访问控制从严:
1) 如果不允许外部直接通过new来创建对象,那么构造方法必须是private。
2) 工具类不允许有public或default构造方法。
3) 类非static成员变量并且与子类共享,必须是protected。
4) 类非static成员变量并且仅在本类使用,必须是private。
5) 类static成员变量如果仅在本类使用,必须是private。
6) 若是static成员变量,必须考虑是否为final。
7) 类成员方法只供类内部调用,必须是private。
8) 类成员方法只对继承类公开,那么限制为protected。
说明:任何类、方法、参数、变量,严控访问范围。过于宽泛的访问范围,不利于模块解耦。思考:如果是一个private的方法,想删除就删除,可是一个public的service方法,或者一个public的成员变量,删除一下,不得手心冒点汗吗?变量像自己的小孩,尽量在自己的视线内,变量作用域太大,无限制的到处跑,那么你会担心的。

集合处理

1. 【强制】关于hashCode和equals的处理,遵循如下规则:
1) 只要重写equals,就必须重写hashCode。
2) 因为Set存储的是不重复的对象,依据hashCode和equals进行判断,所以Set存储的对象必须重写这两个方法。
3) 如果自定义对象做为Map的键,那么必须重写hashCode和equals。
说明:String重写了hashCode和equals方法,所以我们可以非常愉快地使用String对象作为key来使用。

7. 【强制】不要在foreach循环里进行元素的remove/add操作。remove元素请使用Iterator方式,如果并发操作,需要对Iterator对象加锁。
正例:
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
String item = iterator.next();
if (删除元素的条件) {
iterator.remove();
}
} 反例:
List<String> list = new ArrayList<String>();
list.add("1");
list.add("2");
for (String item : list) {
if ("1".equals(item)) {
list.remove(item);
}
}
说明:以上代码的执行结果肯定会出乎大家的意料,那么试一下把“1”换成“2”,会是同样的结果吗?

8. 【强制】 在JDK7版本及以上,Comparator要满足如下三个条件,不然Arrays.sort,Collections.sort会报IllegalArgumentException异常。 说明:三个条件如下 1) x,y的比较结果和y,x的比较结果相反。
2) x>y,y>z,则x>z。 3) x=y,则x,z比较结果和y,z比较结果相同。 反例:下例中没有处理相等的情况,实际使用中可能会出现异常:
new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getId() > o2.getId() ? 1 : -1;
}
};

并发处理
3. 【强制】线程资源必须通过线程池提供,不允许在应用中自行显式创建线程。 说明:使用线程池的好处是减少在创建和销毁线程上所花的时间以及系统资源的开销,解决资源不足的问题。如果不使用线程池,有可能造成系统创建大量同类线程而导致消耗完内存或者“过度切换”的问题。

4. 【强制】线程池不允许使用 Executors ExecutorsExecutors Executors ExecutorsExecutors去创建,而是通过 去创建,而是通过 去创建,而是通过 ThreadPoolExecutor ThreadPoolExecutorThreadPoolExecutor ThreadPoolExecutor ThreadPoolExecutor ThreadPoolExecutor ThreadPoolExecutorThreadPoolExecutor ThreadPoolExecutor ThreadPoolExecutorThreadPoolExecutor的方式,这样 的方式,这样 的处理方式让写同学更加明确线程池运行规则,避资源耗尽风险。 说明: Executors ExecutorsExecutors Executors ExecutorsExecutors返回的线程池对象 返回的线程池对象 的弊端 如下 :
1)FixedThreadPool FixedThreadPool FixedThreadPool FixedThreadPool FixedThreadPool FixedThreadPool FixedThreadPool FixedThreadPool和 SingleThread SingleThreadSingleThread SingleThread SingleThread SingleThread SingleThreadPoolPool Pool: 允许的请求队列长度为 Integer.MAX_VALUE,可 能会堆积大量的请求,从而导致 OOM。
2)CachedThreadPool CachedThreadPool CachedThreadPool CachedThreadPool CachedThreadPool CachedThreadPool CachedThreadPool CachedThreadPool 和 ScheduledThreadPool ScheduledThreadPoolScheduledThreadPool ScheduledThreadPool ScheduledThreadPool ScheduledThreadPool ScheduledThreadPool ScheduledThreadPool ScheduledThreadPool ScheduledThreadPool : 允许的创建线程数量为 Integer.MAX_VALUE,可能会创建大量的线程,从而导致 OOM。

5. 【强制】SimpleDateFormat 是线程不安全的类,一般不要定义为static变量,如果定义为static,必须加锁,或者使用DateUtils工具类。 正例:注意线程安全,使用DateUtils。亦推荐如下处理:
private static final ThreadLocal<DateFormat> df = new ThreadLocal<DateFormat>() {
@Override
protected DateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd");
}
};
说明:如果是JDK8的应用,可以使用Instant代替Date,LocalDateTime代替Calendar,DateTimeFormatter代替SimpleDateFormat,官方给出的解释:simple beautiful strong immutable thread-safe。

6. 【强制】高并发时,同步调用应该去考量锁的性能损耗。能用无锁数据结构,就不要用锁;能锁区块,就不要锁整个方法体;能用对象锁,就不要用类锁。 说明:尽可能使加锁的代码块工作量尽可能的小,避免在锁代码块中调用RPC方法。

8. 【强制】并发修改同一记录时,避免更新丢失,需要加锁。要么在应用层加锁,要么在缓存加锁,要么在数据库层使用乐观锁,使用version作为更新依据。 说明:如果每次访问冲突概率小于20%,推荐使用乐观锁,否则使用悲观锁。乐观锁的重试次数不得小于3次。

其他

1. 【强制】在使用正则表达式时,利用好其预编译功能,可以有效加快正则匹配速度。 说明:不要在方法体内定义:Pattern pattern = Pattern.compile(规则);

3. 【强制】对大段代码进行try-catch,这是不负责任的表现。catch时请分清稳定代码和非稳定代码,稳定代码指的是无论如何不会出错的代码。对于非稳定代码的catch尽可能进行区分异常类型,再做对应的异常处理。

13. 【参考】避免出现重复的代码(Don’t Repeat Yourself),即DRY原则。 说明:随意复制和粘贴代码,必然会导致代码的重复,在以后需要修改时,需要修改所有的副本,容易遗漏。必要时抽取共性方法,或者抽象公共类,甚至是组件化。 正例:一个类中有多个public方法,都需要进行数行相同的参数校验操作,这个时候请抽取:
private boolean checkParam(DTO dto) {...}

7. 【强制】在使用平台资源,譬如短信、邮件、电话、下单、支付,必须实现正确的防重放限制,如数量限制、疲劳度控制、验证码校验,避免被滥刷、资损。 说明:如注册时发送验证码到手机,如果没有限制次数和频率,那么可以利用此功能骚扰到其它用户,并造成短信平台资源浪费。

5. 【强制】 在代码中写分页查询逻辑时,若count为0应直接返回,避免执行后面的分页语句。

6. 【强制】不得使用外键与级联,一切外键概念必须在应用层解决。 说明:以学生和成绩的关系为例,学生表中的student_id是主键,那么成绩表中的student_id则为外键。如果更新学生表中的student_id,同时触发成绩表中的student_id更新,即为级联更新。外键与级联更新适用于单机低并发,不适合分布式、高并发集群;级联更新是强阻塞,存在数据库更新风暴的风险;外键影响数据库的插入速度。

7. 【强制】禁止使用存储过程,存储过程难以调试和扩展,更没有移植性。

10. 【参考】如果有全球化需要,所有的字符存储与表示,均以utf-8编码,注意字符统计函数的区别。 说明: SELECT LENGTH("轻松工作"); 返回为12 SELECT CHARACTER_LENGTH("轻松工作"); 返回为4 如果需要存储表情,那么选择utfmb4来进行存储,注意它与utf-8编码的区别。


完整版pdf下载链接 https://download.csdn.net/download/fu250/10308874  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325577339&siteId=291194637