未验证 提交 1b5763a3 编写于 作者: 玄坛 提交者: GitHub

Merge pull request #479 from kerie/readme-rules

update new rules in readme
......@@ -156,27 +156,28 @@ Positive example: AbstractTranslator to implement Translatable.
Counter example: String key="Id#taobao_" + tradeId;
* 2 ``[Mandatory]`` 'L' instead of 'l' should be used for long or Long variable because 'l' is easily to be regarded as number 1 in mistake.
Counter example: Long a=2l, it is hard to tell whether it is number 21 or Long 2.
### <font color="green">OOP</font>
* 3 ``[Mandatory]`` Using a deprecated class or method is prohibited.
* 1 ``[Mandatory]`` Using a deprecated class or method is prohibited.
Note: For example, decode(String source, String encode) should be used instead of the deprecated method decode(String encodeStr). Once an interface has been deprecated, the interface provider has the obligation to provide a new one. At the same time, client programmers have the obligation to check out what its new implementation is.
* 4 ``[Mandatory]`` Since NullPointerException can possibly be thrown while calling the equals method of Object, equals should be invoked by a constant or an object that is definitely not null.
* 2 ``[Mandatory]`` Since NullPointerException can possibly be thrown while calling the equals method of Object, equals should be invoked by a constant or an object that is definitely not null.
Positive example: "test".equals(object);
Counter example: object.equals("test");
Note: java.util.Objects#equals (a utility class in JDK7) is recommended.
* 5 ``[Mandatory]`` The wrapper classes should be compared by equals method rather than by symbol of '==' directly.
* 3 ``[Mandatory]`` The wrapper classes should be compared by equals method rather than by symbol of '==' directly.
Note: Consider this assignment: Integer var = ?. When it fits the range from -128 to 127, we can use == directly for a comparison. Because the Integer object will be generated by IntegerCache.cache, which reuses an existing object. Nevertheless, when it fits the complementary set of the former range, the Integer object will be allocated in Heap, which does not reuse an existing object. This is an [implementation-level detail](https://docs.oracle.com/javase/specs/jls/se9/html/jls-5.html#jls-5.1.7-300) that should NOT be relied upon. Hence using the equals method is always recommended.
* 6 ``[Mandatory]`` Rules for using primitive data types and wrapper classes:
* 4 ``[Mandatory]`` Rules for using primitive data types and wrapper classes:
1. Members of a POJO class must be wrapper classes.
2. The return value and arguments of a RPC method must be wrapper classes.
3. ``[Recommended]`` Local variables should be primitive data types.
Note: In order to remind the consumer of explicit assignments, there are no initial values for members in a POJO class. As a consumer, you should check problems such as NullPointerException and warehouse entries for yourself.
 Positive example: As the result of a database query may be null, assigning it to a primitive date type will cause a risk of NullPointerException because of Unboxing.
 Counter example: Consider the output of a transaction volume's amplitude, like ±x%. As a primitive data, when it comes to a failure of calling a RPC service, the default return value: 0% will be assigned, which is not correct. A hyphen like - should be assigned instead. Therefore, the null value of a wrapper class can represent additional information, such as a failure of calling a RPC service, an abnormal exit, etc.
* 7 ``[Mandatory]`` While defining POJO classes like DO, DTO, VO, etc., do not assign any default values to the members.
* 8 ``[Mandatory]`` The toString method must be implemented in a POJO class. The super.toString method should be called in front of the whole implementation if the current class extends another POJO class.
* 5 ``[Mandatory]`` While defining POJO classes like DO, DTO, VO, etc., do not assign any default values to the members.
* 6 ``[Mandatory]`` The toString method must be implemented in a POJO class. The super.toString method should be called in front of the whole implementation if the current class extends another POJO class.
Note: We can call the toString method in a POJO directly to print property values in order to check the problem when a method throws an exception in runtime.
* 9 ``[Recommended]`` Use the append method in StringBuilder inside a loop body when concatenating multiple strings.
* 7 ``[Recommended]`` Use the append method in StringBuilder inside a loop body when concatenating multiple strings.
Counter example:
......@@ -215,11 +216,13 @@ Positive example:
// ...
}
```
* 4 ``[Recommended]`` Avoid using the negation operator '!'.
Note: The negation operator is not easy to be quickly understood. There must be a positive way to represent the same logic.
### <font color="green">Exception</font>
* 4 ``[Mandatory]`` Make sure to invoke the rollback if a method throws an Exception. Rollbacks are based on the context of the coding logic.
* 5 ``[Mandatory]`` Never use return within a finally block. A return statement in a finally block will cause exceptions or result in a discarded return value in the try-catch block.
* 6 ``[Recommended]`` One of the most common errors is NullPointerException. Pay attention to the following situations:
* 1 ``[Mandatory]`` Make sure to invoke the rollback if a method throws an Exception. Rollbacks are based on the context of the coding logic.
* 2 ``[Mandatory]`` Never use return within a finally block. A return statement in a finally block will cause exceptions or result in a discarded return value in the try-catch block.
* 3 ``[Recommended]`` One of the most common errors is NullPointerException. Pay attention to the following situations:
* 1 If the return type is primitive, return a value of wrapper class may cause NullPointerException.
  Counter example: public int f() { return Integer } Unboxing a null value will throw a NullPointerException.
* 2 The return value of a database query might be null.
......@@ -236,6 +239,7 @@ Note: In IDE, Javadoc can be seen directly when hovering, which is a good way to
* 3 ``[Mandatory]`` Every class should include information of author(s) and date.
* 4 ``[Mandatory]`` Single line comments in a method should be put above the code to be commented, by using // and multiple lines by using /* */. Alignment for comments should be noticed carefully.
* 5 ``[Mandatory]`` All enumeration type fields should be commented as Javadoc style.
* 6 ``[Recommended]`` Codes or configuration that is noticed to be obsoleted should be resolutely removed from projects.
### <font color="green">Other</font>
......@@ -247,3 +251,21 @@ Note: If attribute is null or does not exist, ${var} will be shown directly on w
* 4 ``[Mandatory]`` The return type of Math.random() is double, value range is 0<=x<1 (0 is possible). If a random integer is required, do not multiply x by 10 then round the result. The correct way is to use nextInt or nextLong method which belong to Random Object.
* 5 ``[Mandatory]`` Use System.currentTimeMillis() to get the current millisecond. Do not use new Date().getTime().
Note: In order to get a more accurate time, use System.nanoTime(). In JDK8, use Instant class to deal with situations like time statistics.
* 6 ``[Mandatory]`` When doing date formatting, "y" should be written in lowercase for "year" in a pattern statement.
Note: When doing date formatting, "yyyy" represents the day in which year, while "YYYY" represents the week in which year (a concept introduced in JDK7). If a week is across two years, the returning "YYYY"represents the next year.
Some more points need to be notices:
* Uppercase "M" stands for month.
* Lowercase "m" stands for minute.
* Uppercase "H" stands for 24-hour clock.
* Lowercase "h" stands for 12-hour clock.
Positive Example: Example pattern for date formatting:
```java
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
```
Counter Example: Someone applied "YYYY/MM/dd" pattern for date formatting, and the execution result of 2017/12/31 was 2018/12/31, leading to a serious failure.
* 7 ``[Recommended]`` The total number of lines for a method should not be more than 80.
Note: The total number of lines, including the method signature, closing brace, codes, comments, blank lines, line breaks and any invisible lines, should not be more than 80.
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册