Java code specification

1、 Foreword

This article refers to Alibaba java development manual, which mainly defines some code specifications and some precautions. I only excerpted some contents according to my own shortcomings for future reference.

2、 Reading notes

1. No naming in the code can start or end with an underscore or dollar sign.

1. The sublist result of ArrayList cannot be forcibly converted to ArrayList, otherwise ClassCastException will be thrown: Java util. RandomAccessSubList cannot be cast to java. util. ArrayList。 Sublist returns the internal class sublist of ArrayList, not ArrayList, but a view of ArrayList. All operations on sublist sublist will eventually be reflected on the original list.

2. In the sublist scenario, pay great attention to the modification of the number of elements in the original set, which will lead to the concurrent modificationexception exception in the traversal, addition and deletion of the sublist.

3. To use the method of converting a collection to an array, you must use the toArray (t [] array) of the collection. The passed in array is an array of exactly the same type, and the size is list size()。

4. Use the tool class arrays When aslist() converts an array into a collection, it cannot be used to modify the collection related methods. Its add / remove / clear method will throw an unsupported operationexception.

5. Generic wildcard to receive the returned data. The generic collection of this writing method cannot use the add method, but cannot use the get method, which is prone to error when assigning values to interface calls.

6. Do not remove / add elements in a foreach loop. For the remove element, please use the iterator method. If concurrent operations occur, you need to lock the iterator object.

7. Specifies the initial value size of the collection when the collection is initialized.

9. Use entryset to traverse the map class set kV instead of keyset.

10. In the Java collection class library, the general time complexity of the contains method of list is O (n). If you need to call the contains method frequently to find data in the code, you can first convert the list into a HashSet implementation to reduce the time complexity of O (n) to o (1).

11. Pay attention to whether the map class set K / V can store null values, as shown in the following table:

1. Braces must be used in if / else / for / while / do statements. Even if there is only one line of code, avoid using the single line form: if (condition) statements;

2. When expressing abnormal branches, use less if else. If you have to use if () else if()... else... Express logic in a way to avoid difficulties in subsequent code maintenance. Do not exceed 3 layers.

3. Except for common methods (such as getxxx / isXXX), do not execute other complex statements in condition judgment, and assign the result of complex logic judgment to a meaningful boolean variable name to improve readability.

1. A kind of runtimeException defined in the Java class library can be circumvented through pre inspection rather than catch, such as indexoutofboundsexception, NullPointerException, etc.

1. The API in the log system (log4j, logback) cannot be directly used in the application, but the API in the log framework slf4j should be used. The log framework in facade mode is used, which is conducive to the unification of maintenance and log processing methods of various classes.

2. It is recommended that the log file be kept for at least 15 days, because some exceptions occur frequently in "week".

3. For trace / debug / Info level log output, you must use conditional output or placeholder.

4. Avoid printing logs repeatedly and wasting disk space. Be sure to print logs in log4j Set additivity = false in XML.

5. Log carefully. The production environment prohibits the output of debug logs; Selectively output the info log; If you use warn to record the business behavior information when you first go online, you must pay attention to the problem of log output, avoid bursting the server disk, and remember to delete these observation logs in time.

1. When using regular expressions, making good use of its precompiling function can effectively speed up the regular matching speed.

2. The variables sent to the page in the background must be added with $! {var} - exclamation point in the middle.

3. Do not add any complex logic to the view template.

4. The size of any data structure shall be specified for construction or initialization to avoid unlimited growth of data structure and eating up memory.

5. For "code and configuration that are explicitly out of use", such as methods, variables, classes, configuration files, dynamic configuration properties, etc., we should resolutely clean them out of the program to avoid causing too much garbage.

6. New BigDecimal (double) has the risk of accuracy loss, which may lead to business logic exceptions in the scenario of accurate calculation or value comparison. Please use BigDecimal valueOf(double) 。

7. Simpledateformat is a thread unsafe class. Generally, it should not be defined as a static variable. If it is defined as static, it must be locked, or use the dateutils tool class.

Book link: Alibaba java development manual

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>