Joke Collection Website - Blessing messages - Why do Alibaba engineers write good code? Just look at his code specifications and you will know
Why do Alibaba engineers write good code? Just look at his code specifications and you will know
I once worked with a Java engineer from Alibaba for a period of time. His technology cannot be said to be very powerful, but his code is very good. There are very few bugs in the functions you make. I was very curious, so I often asked him for some code design principles, and then he told me the Alibaba Java Manual. Moreover, he modified this manual and it became a development manual for our Java programmers. In this article, let us take a look at the more important principles in this manual.
Forces that names in the code cannot start with, or end with, an underscore or dollar sign. .
Counterexample: _name / __name / $name / name_ / name$ / name__
Mandatory types are connected closely with square brackets to represent arrays.
Positive example: define an integer array int[] arrayDemo; Counterexample: use String args[] to define in the main parameter.
Force Boolean type variables in POJO classes not to be prefixed with is, otherwise some framework parsing will cause serialization errors.
Note: The value expressing yes or no adopts the naming method of is_xxx, so the mapping relationship from is_xxx to xxx needs to be set in .
Counterexample: An attribute defined as the basic data type Boolean isDeleted, and its method is also isDeleted(). When the RPC framework performs reverse parsing, it "mistakes" that the corresponding attribute name is deleted, resulting in attribute acquisition If not, an exception will be thrown.
Recommendation: When naming constants and variables, put the noun indicating the type at the end of the word to improve recognition.
It is recommended that the methods and properties in the interface class do not add any modification symbols (also do not add public), keep the code simple, and add Valid Javadoc comments. Try not to define variables in the interface. If you must define variables, they must be related to interface methods and are basic constants for the entire application.
Positive example: interface method signature void commit();
Interface basic constant String COMPANY = "alibaba";
Counterexample: interface method definition public abstract void f();
Note: Interfaces in JDK8 allow default implementations, so this default method is a valuable default implementation for all implementation classes.
Reference The enumeration class name has the Enum suffix, and the enumeration member names need to be in all uppercase letters, with underscores separating words.
Note: Enumerations are actually special classes. Domain members are all constants, and the constructor is forced to be private by default.
Positive example: The member name of the enumeration named ProcessStatusEnum: SUCCESS / UNKNOWN_REASON.
Reference naming convention for each layer:
1) The method of obtaining a single object is prefixed with get.
2) Methods to obtain multiple objects are prefixed with list, and the plural form ends in: listObjects. 3) The method of obtaining statistical values ??is prefixed with count.
4) The insertion method is prefixed with save/insert.
5) The deletion method is prefixed with remove/delete.
6) The modification method is prefixed with update.
1) Data object: xxxDO, xxx is the name of the data table.
2) Data transfer object: xxxDTO, xxx is the name related to the business field.
3) Display object: xxxVO, xxx is generally the name of the web page.
4) POJO is the collective name of DO/DTO/BO/VO, and it is forbidden to name it xxxPOJO.
Enforcement No magic values ??(that is, constants that are not predefined) are allowed to appear directly in the code.
Mandatory Avoid accessing static variables or static methods of this class through object references of a class, which will unnecessarily increase compiler parsing costs. Just access them directly with the class name.
Mandatory Java variable parameters can be used only if they have the same parameter type and the same business meaning. Avoid using Object.
Note: Variable parameters must be placed at the end of the parameter list. (Students are encouraged not to use variable parameter programming as much as possible)
Mandatory All comparisons of values ??between integer packaging class objects should be compared using the equals method.
Note: For Integer var = ? assignment in the range of -128 to 127, the Integer object is generated in IntegerCache.cache and existing objects will be reused. Integer values ??in this range can be used directly = = to judge, but all data outside this range will be generated on the heap and existing objects will not be reused. This is a big pitfall. It is recommended to use the equals method to judge.
The usage standards for basic data types and packaged data types are as follows:
Note: POJO class attributes do not have initial values ??to remind users when they need to use them. , you must explicitly assign the value yourself, and any NPE issues or storage checks are ensured by the user.
Positive example: The query result of the database may be null because automatic unboxing and receiving with basic data types have NPE risks.
Counter-example: For example, display the rise and fall of the total transaction amount, that is, plus or minus x%, x is the basic data type, and the RPC service called, when the call is unsuccessful, returns the default value, and the page displays 0% , which is unreasonable and should be displayed as a dash. Therefore, the null value of the wrapped data type can represent additional information, such as: remote call failure and abnormal exit.
Force POJO class to write toString method.
When using the tool in the IDE: source> generate toString, if you inherit another POJO class, be sure to add super.toString in front.
Note: When an exception is thrown during method execution, you can directly call POJO's toString() method to print its attribute values ??to facilitate troubleshooting.
Force the processing of hashCode and equals to follow the following rules:
Description: String has overridden the hashCode and equals methods, so we can happily use them String objects are used as keys.
Mandatory thread resources must be provided through the thread pool, and explicit creation of threads in the application is not allowed.
Description: The benefit of the thread pool is to reduce the time spent on 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, leading to memory consumption or "excessive switching" problems.
Forced thread pools are not allowed to be created using Executors, but through ThreadPoolExecutor. This processing method allows the writing students to be more clear about the running rules of the thread pool and avoid resource consumption. All risks.
The above specifications are important principles in designing code. If you follow the above principles when writing code, the readability and maintainability of the code will be greatly improved
- Previous article:How to set the font size of SMS?
- Next article:Qingyang traffic violation inquiry
- Related articles
- How to thank the boss for giving bonuses privately?
- I want to be an online novel writer. What should I do?
- Holding company of Gulf Group
- What should I say when texting after dating a girl?
- Someone sent me a message wishing me a happy new year. What does this mean?
- How to comfort your boyfriend when he is angry?
- A letter of thanks to the police! (high reward)
- Why is there no SMS prompt on the mobile phone?
- What's the matter with receiving the taxi charge notice from Meituan?
- A sentence implying disappointment with the leader