提交 7acc9657 编写于 作者: A A S Gowri Sankar 提交者: GitHub

Update README.MD for minor fixes.

1. Grammatical fixes. 2. Markdown changes.
上级 6922e49f
......@@ -24,7 +24,7 @@ compile 'com.alibaba.p3c:p3c-pmd:1.3.0'
P3C-PMD implements 49 rules involved in *Alibaba Java Coding Guidelines*, based on PMD ([https://github.com/pmd/pmd](https://github.com/pmd/pmd)).
### <font color="green">Concurrency</font>
* 1 ``[Mandatory]`` Customized ThreadLocal variables must be recycled,especially when using thread pools in which threads are often reused. Otherwise, it may affect subsequent business logic and cause unexpected problems such as memory leak.
* 1 ``[Mandatory]`` Customized ThreadLocal variables must be recycled, especially when using thread pools in which threads are often reused. Otherwise, it may affect subsequent business logic and cause unexpected problems such as memory leak.
* 2 ``[Mandatory]`` A meaningful thread name is helpful to trace the error information, so assign a name when creating threads or thread pools.
Positive example:
......@@ -35,14 +35,14 @@ Positive example:
```
* 3 ``[Mandatory]`` Threads should be provided by thread pools. Explicitly creating threads is not allowed.
Note: Using thread pool can reduce the time of creating and destroying thread and save system resource. If we do not use thread pools, lots of similar threads will be created which lead to "running out of memory" or over-switching problems.
* 4 ``[Mandatory]`` A thread pool should be created by ThreadPoolExecutor rather than Executors. These would make the parameters of the thread pool understandable. It would also reduce the risk of running out of system resource.
* 4 ``[Mandatory]`` A thread pool should be created by ThreadPoolExecutor rather than Executors. These would make the parameters of the thread pool understandable. It would also reduce the risk of running out of system resources.
Note: Below are the problems created by usage of Executors for thread pool creation:
1. FixedThreadPool and SingleThreadPool:
  Maximum request queue size Integer.MAX_VALUE. A large number of requests might cause OOM.
2. CachedThreadPool and ScheduledThreadPool:
The number of threads which are allowed to be created is Integer.MAX_VALUE. Creating too many threads might lead to OOM.
* 5 ``[Mandatory]`` SimpleDataFormat is unsafe, do not define it as a static variable. If have to, lock or DateUtils class must be used.
Positive example: Pay attention to thread-safety when using DateUtils. It is recommended to use as below:
* 5 ``[Mandatory]`` SimpleDataFormat is unsafe, do not define it as a static variable. If you have to, lock or Apache DateUtils class must be used.
Positive example: Pay attention to thread-safety when using DateUtils. It is recommended to use below:
```
private static final ThreadLocal<DateFormat> df = new ThreadLocal<DateFormat>() {
......@@ -52,21 +52,21 @@ Positive example: Pay attention to thread-safety when using DateUtils. It is rec
}
};
```
Note: In JDK8, Instant can be used to replace Date, Calendar is replaced by LocalDateTime, SimpleDateFormatter is replaced by DateTimeFormatter.
* 10 ``[Mandatory]`` Run multiple TimeTask by using ScheduledExecutorService rather than Timer because Timer will kill all running threads in case of failing to catch exceptions.
* 11 ``[Recommended]`` When using CountDownLatch to convert asynchronous operations to synchronous ones, each thread must call countdown method before quitting. Make sure to catch any exception during thread running, to let countdown method be executed. If main thread cannot reach await method, program will return until timeout.
Note: Be careful, exception thrown by sub-thread cannot be caught by main thread.
* 12 ``[Recommended]`` Avoid using Random instance by multiple threads. Although it is safe to share this instance, competition on the same seed will damage performance.
Note: In JDK8, Instant can be used to replace Date; likewise Calendar is replaced by LocalDateTime, and SimpleDateFormatter is replaced by DateTimeFormatter.
* 6 ``[Mandatory]`` Run multiple TimeTask by using ScheduledExecutorService rather than Timer, because Timer will kill all running threads in case of failure to catch exceptions.
* 7 ``[Recommended]`` When using CountDownLatch to convert asynchronous operations to synchronous ones, each thread must call countdown method before quitting. Make sure to catch any exception during thread running, to let countdown method be executed. If main thread cannot reach await method, program will return until timeout.
Note: Be careful, exception thrown by a child thread cannot be caught by main thread.
* 8 ``[Recommended]`` Avoid using Random instance by multiple threads. Although it is thread-safe, competition on the same seed will damage performance.
Note: Random instance includes instances of java.util.Random and Math.random().
Positive example:
After JDK7, API ThreadLocalRandom can be used directly. But before JDK7, instance needs to be created in each thread.
After JDK7, ThreadLocalRandom API can be used directly. But before JDK7, instance needs to be created in each thread.
### <font color="green">Collection</font>
* 4 ``[Mandatory]`` Do not cast subList in class ArrayList, otherwise ClassCastException will be thrown: java.util.RandomAccessSubList cannot be cast to java.util.ArrayList.
* 1 ``[Mandatory]`` Do not cast subList in class ArrayList, otherwise ClassCastException will be thrown: java.util.RandomAccessSubList cannot be cast to java.util.ArrayList.
Note: subList of ArrayList is an inner class, which is a view of ArrayList. All operations on the Sublist will affect the original list finally.
* 5 ``[Mandatory]`` When using subList, be careful to modify the size of original list. It might cause ConcurrentModificationException when performing traversing, adding or deleting on the subList.
* 6 ``[Mandatory]`` Use toArray(T[] array) to convert list to array. The input array type should be the same with the list whose size is list.size().
* 2 ``[Mandatory]`` When using subList, be careful while modifying the size of original list. It might cause ConcurrentModificationException when performing traversing, adding or deleting on the subList.
* 3 ``[Mandatory]`` Use toArray(T[] array) to convert list to array. The input array type should be the same with the list whose size is list.size().
Counter example: Do not use toArray method without arguments. Since the return type is Object[], ClassCastException will be thrown when casting it to a different array type.
Positive example:
......@@ -78,7 +78,7 @@ Positive example:
array = list.toArray(array);
```
Note: When using toArray method with arguments, if input array size is not large enough, the method will re-assign the size internally, and then return the address of new array. If the size is larger than needed, the value of index[list.size()] will be set to null while other values remain the same. Defining an input with the same size of the list is recommended.
* 7 ``[Mandatory]`` Do not use methods which will modify the list after using Arrays.asList to convert array to list, otherwise methods like add/remove/clear will throw UnsupportedOperationException.
* 4 ``[Mandatory]`` Do not use methods which will modify the list after using Arrays.asList to convert array to list, otherwise methods like add/remove/clear will throw UnsupportedOperationException.
Note: The result of asList is the inner class of Arrays, which does not implement methods to modify itself. Arrays.asList is only a transferred interface, data inside which is stored as an array.
```
......@@ -87,7 +87,7 @@ Note: The result of asList is the inner class of Arrays, which does not implemen
```
Case 1: list.add("c"); will throw a runtime exception.
Case 2: str[0]= "gujin"; list.get(0) will be modified.
* 9 ``[Mandatory]`` Do not remove or add elements to a collection in a foreach loop. Please use Iterator to remove an item. Iterator object should be synchronized when executing concurrent operations.
* 5 ``[Mandatory]`` Do not remove or add elements to a collection in a foreach loop. Please use Iterator to remove an item. Iterator object should be synchronized when executing concurrent operations.
Counter example:
```
......@@ -112,12 +112,12 @@ Positive example:
}
}
```
* 11``[Recommended]`` Set a size when initializing a collection if possible.
* 6``[Recommended]`` Set a size when initializing a collection if possible.
Note: Better to use ArrayList(int initialCapacity) to initialize ArrayList.
### <font color="green">Naming Conventions</font>
* 1 ``[Mandatory]`` All names should not start or end with an underline or a dollar sign.
* 1 ``[Mandatory]`` No identifier name should 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.
......@@ -134,16 +134,17 @@ Positive example: localValue / getHttpMessage() / inputUserId
Positive example: MAX_STOCK_COUNT
Counter example: MAX_COUNT
* 6 ``[Mandatory]`` Abstract class names must start with Abstract or Base. Exception class names must be ended with Exception. Test cases shall be started with the class names to be tested and ended with Test.
* 6 ``[Mandatory]`` Abstract class names must start with Abstract or Base. Exception class names must end with Exception. Test cases 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]`` 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 name can be in plural format if necessary.
Positive example: com.alibaba.open.util can be used as package name for utils;
* 13 There are mainly two rules for interface and corresponding implementation class naming:
* 10 There are mainly two rules for interface and corresponding implementation class naming:
1. ``[Mandatory]`` All Service and DAO classes must be interface based on SOA principle. Implementation class names should be ended with Impl.
Positive example: CacheServiceImpl to implement CacheService.
2. ``[Recommended]`` If the interface name is to indicate the ability of the interface, then its name should be adjective.
......@@ -156,26 +157,26 @@ 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>
* 5 ``[Mandatory]`` Using a deprecated class or method is prohibited.
* 3 ``[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.
* 6 ``[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.
* 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.
Positive example: "test".equals(object);
Counter example: object.equals("test");
Note: java.util.Objects#equals (a utility class in JDK7) is recommended.
* 7 ``[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 a pitfall. Hence the equals method is recommended.
* 8 ``[Mandatory]`` Rules for using primitive data types and wrapper classes:
* 5 ``[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:
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 autoboxing.
 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.
* 9 ``[Mandatory]`` While defining POJO classes like DO, DTO, VO, etc., do not assign any default values to the members.
* 12 ``[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.
* 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.
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.
* 17 ``[Recommended]`` Use the append method in StringBuilder inside a loop body when concatenating multiple strings.
* 9 ``[Recommended]`` Use the append method in StringBuilder inside a loop body when concatenating multiple strings.
Counter example:
......@@ -186,7 +187,7 @@ Note: We can call the toString method in a POJO directly to print property value
}
```
Note: According to the decompiled bytecode file, for each loop, it allocates a StringBuilder object, appends a string, and finally returns a String object via the toString method. This is a tremendous waste of memory.
Note: According to the decompiled bytecode file, for each iteration, it allocates a new StringBuilder object, appends a string, and finally returns a String object via the toString method. This is a tremendous waste of memory, especially when the iteration count is large.
### <font color="green">Flow Control Statements</font>
* 1 ``[Mandatory]`` In a switch block, each case should be finished by break/return. If not, a note should be included to describe at which case it will stop. Within every switch block, a default statement must be present, even if it is empty.
......@@ -195,8 +196,8 @@ Note: We can call the toString method in a POJO directly to print property value
```
if (condition) statements;
```
* 4 ``[Recommended]`` Do not use complicated statements in conditional statements (except for frequently used methods like getXxx/isXxx). Use boolean variables to store results of complicated statements temporarily will increase the code's readability.
Note: Logic within many if statements are very complicated. Readers need to analyze the final results of the conditional expression to decide what statement is to be executed in certain conditions.
* 3 ``[Recommended]`` Do not use complicated expressions in conditional statements (except for frequently used methods like getXxx/isXxx). Using boolean variables to store results of complicated expressions temporarily will increase the code's readability.
Note: Logic within many if statements are very complicated. Readers need to analyze the final results of the conditional expression to understand the branching logic.
Positive example:
```
......@@ -216,16 +217,16 @@ Positive example:
```
### <font color="green">Exception</font>
* 5 ``[Mandatory]`` Make sure to invoke the rollback if a method throws an Exception
* 7 ``[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.
* 10 ``[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.
* 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 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.
3. Elements in collection may be null, even though Collection.isEmpty() returns false.
4. Return values from an RPC might be null.
5. Data stored in sessions might by null.
6. Method chaining, like obj.getA().getB().getC(), is likely to cause NullPointerException.
* 2 The return value of a database query might be null.
* 3 Elements in collection may be null, even though Collection.isEmpty() returns false.
* 4 Return values from an RPC might be null.
* 5 Data stored in sessions might by null.
* 6 Method chaining, like obj.getA().getB().getC(), is likely to cause NullPointerException.
  Positive example: Use Optional to avoid null check and NPE (Java 8+).
### <font color="green">Code Comments</font>
......@@ -238,8 +239,8 @@ Note: In IDE, Javadoc can be seen directly when hovering, which is a good way to
### <font color="green">Other</font>
* ``[Mandatory]`` Avoid using *Apache Beanutils* to copy attributes.
* 1 ``[Mandatory]`` When using regex, precompile needs to be done in order to increase the matching performance.
* 1``[Mandatory]`` Avoid using *Apache Beanutils* to copy attributes.
* 2 ``[Mandatory]`` When using regex, precompile needs to be done in order to increase the matching performance and preferably stored as a constant.
Note: Do not define Pattern pattern = Pattern.compile(.); within method body.
* 3 ``[Mandatory]`` Variables must add exclamatory mark when passing to velocity engine from backend, like $!{var}.
Note: If attribute is null or does not exist, ${var} will be shown directly on web pages.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册