Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Why is Ali's code well written? Just look at his code specification.
Why is Ali's code well written? Just look at his code specification.
I once worked with a Java engineer in Ali for a while. His technology is not very powerful, but his code is well written, and all functions have few bugs. I am very curious, so I often ask him some principles of code design, and then he tells me the Ali Java manual. Moreover, he revised this manual and became the development manual for our Java programmers. In this article, let's look at the more important principles in this manual.

Naming in mandatory codes cannot start or end with an underscore or dollar sign.

Counterexample: _ name/_ _ name/$ name/name _/name $/name _ _

Forces types to be immediately bracketed to represent arrays.

Positive example: define integer array int [] arraydemo; Counterexample: in the main parameter, the string args[] is used to define it.

Force Boolean variables in POJO classes not to be prefixed with is, otherwise some frame parsing will lead to serialization errors.

Note: The value indicating yes or no is named is_xxx, so you need to set the mapping relationship from is_xxx to xxx.

Counterexample: the attribute defined as the basic data type Boolean isDeleted, and its method is also deleted (). During the reverse parsing of RPC framework, the corresponding attribute name was "mistakenly thought" to be deleted, resulting in the attribute not being obtained, and then an exception was thrown.

It is suggested that when naming constants and variables, nouns representing types should be placed at the end of words to improve recognition.

It is suggested that the methods and properties in the interface class should not be decorated with any symbols (nor should they be public), keep the code simple, and add effective Javadoc comments. Try not to define variables in the interface. If a variable must be defined, it must be related to the interface method and be the basic constant of the whole application.

Positive example: interface method signature Voidcommit ();

Interface basic constant string COMPANY = "alibaba

Counterexample: the interface method defines the public abstract void f ();

Description: The interface in JDK8 allows a default implementation, so this default method is a valuable default implementation for all implementation classes.

The suffix of the reference enumeration class name is enum, and the names of enumeration members need to be all capitalized, and words are separated by underscores.

Description: Enumeration is actually a special class, the domain members are constants, and the constructor is forced to be private by default.

Positive example: enumerate the member names named processstatusenum: success/unknown _ reason.

Refer to the naming convention of each layer:

1) The method of getting a single object is prefixed with get.

2) The method of obtaining multiple objects is prefixed with list, and the plural form ends with: listObjects. 3) The method of obtaining statistical values is prefixed with count.

4) The insert method is prefixed with Save/Insert.

5) Delete method is prefixed with remove/delete.

6) The modified method is prefixed with update.

1) data object: xxxDO, xxx is the name of the data table.

2) Data transmission object: xxxDTO, xxx is the name related to the business field.

3) Display object: xxxVO, xxx is generally the name of the webpage.

4) POJO is the collective name of DO/DTO/BO/VO, and it is forbidden to be named xxxPOJO.

Force the magic value (that is, undefined constant) not to appear directly in the code.

It is mandatory to avoid accessing static variables or static methods of a class through its object reference, which unnecessarily increases the parsing cost of the compiler and can be accessed directly with the class name.

You can use Java variable parameters and avoid using Object only if the mandatory parameter types are the same and the business meanings are the same.

Note: Variable parameters must be placed at the end of the parameter list. (Encourage students to try not to program with variable parameters)

Forces comparison of values between all integer wrapper class objects, and they all use the equals method.

Note: For integer var =? For assignments in the range of-128 to 127, integer objects are generated in IntegerCache.cache, and existing objects will be reused. Integer values in this interval can be directly judged by = =, but data outside this interval will be generated on the heap, and existing objects will not be reused. This is a big pit. equals method is recommended.

The usage standards of basic data types and packaged data types are as follows:

Note: POJO class attributes have no initial value, which reminds users that they must be explicitly assigned when they need to be used. Any NPE problem or incoming inspection is guaranteed by the user.

Positive example: the query result of the database may be empty, because there is NPE risk in automatically unpacking and receiving with basic data types.

Counterexample: For example, it shows the fluctuation of total trading volume, that is, plus or minus x%, where x is the basic data type. When the RPC service call is unsuccessful, the default value is returned, and the page is displayed as 0%, which is unreasonable and should be displayed as a dash. Therefore, a null value of the wrapper data type can represent additional information, such as remote call failure and abnormal exit.

Force POJO classes to write toString methods.

When using the tool in IDE: source & gt to generate toString, if you inherit another POJO class, pay attention to adding super.toString in front of it.

Note: When a method throws an exception, you can directly call POJO's toString () method to print its attribute value, which is convenient for troubleshooting.

Forcing hashCode and equals follows the following rules:

Description: String covers hashCode and equals methods, so we can happily use String objects as keys.

Force thread resources to be provided through the thread pool, and it is not allowed to explicitly create threads in the application.

Description: The advantage of thread pool is to reduce the time of creating and destroying threads and the overhead of system resources, and solve the problem of insufficient resources. If the thread pool is not used, it may cause the system to create a large number of similar threads, which may lead to memory exhaustion or "over-switching".

It is not allowed to use Executors to create a forced thread pool, but use ThreadPoolExecutor instead. Students who write in this way can be more aware of the running rules of the thread pool and avoid the risk of running out of resources.

The above specifications are important principles for designing code. If the above principles can be followed in the coding process, the readability and maintainability of the code will be greatly improved.