"Ali Development Manual" reading notes (2)

4.10 10. [Mandatory] When adding new properties to the serialization class, please do not modify the serialVersionUID field to avoid deserialization failure ; if it
is completely incompatible with the upgrade to avoid deserialization confusion, please modify the
serialVersionUID value.

Note: Note that serialVersionUID inconsistency will throw a serialization runtime exception.


4.11 11. [Mandatory] It is forbidden to add any business logic in the constructor. If there is initialization logic, please put it in the init method.


4.12 12. [Mandatory] The POJO class must write the toString method. When using the IDE 's middle tool: source > generate toString
, if you inherit another POJO class, pay attention to adding super . toString in front . Note: When the method execution throws an exception, you can directly call the toString() method of POJO to print its attribute value, which is convenient for troubleshooting .



4.15 15. [Recommended] The order in which methods are defined in a class is: public method or protected method > private method > getter / setter
method.
Note: public methods are the methods that the callers and maintainers of the class care about the most, and are best displayed above the fold ; although protected methods are only concerned by subclasses
, they may also be
the core methods in the " template design mode " ; while private methods are generally not externally Need special attention, it is a black box implementation ; because the value of method information is low, all getter / setter methods of Service and DAO are placed at the end .



4.16 16. [Recommended] In the setter method, the parameter name is the same as the class member variable name, this . member name = parameter name. In the
getter / setter method, try not to add business logic to increase the difficulty of troubleshooting.
Counter example:
public Integer getData(){
    if(true) {
        return data + 100;
    } else {
        return data - 100;
    }

}


4.17 17. [Recommended] In the loop body, use the append method of StringBuilder to extend the connection method of strings. Counter example: String str = "start"; for(int i=0; i<100; i++){         str = str + "hello"; } Description: The decompiled bytecode file shows that each loop will generate a new one StringBuilder object, then make






The append operation, and finally returns the String object through the toString method , resulting in a waste of memory resources.


4.18 18. [Recommendation] Final can improve the efficiency of program response. If it is declared as final :
1 ) Variables that do not need to be reassigned, including class attributes and local variables.
2 ) Add final before the object parameter , indicating that it is not allowed to modify the reference point. 3 ) Class methods are definitely not allowed to be overridden.


4.19 19. [Recommended] Use the clone method of Object with caution to copy objects. Note: The clone method of the object is a shallow copy by default. If you want to implement a deep copy, you need to override the clone method to copy the attribute object .



4.20 20. [Recommendation] The access control of class members and methods is strict:
1 ) If external objects are not allowed to be created directly through new , the constructor must be
2 ) Utility classes are not allowed to have public or default constructors.
3 ) Class non- static member variables and shared with subclasses must be protected .
4 ) Class non- static member variables and only used in this class must be private .
5 ) If the class static member variable is only used in this class, it must be private .
6 ) If it is a static member variable, it must be considered whether it is final .
7 ) Class member methods are only for internal calls of the class and must beprivate .
8 ) Class member methods are only exposed to inherited classes, so they are limited to protected .
Description: Any class, method, parameter, variable, strictly control the access scope. Excessive access to
the test: If it is a
private method, delete it if you want to delete it, but if you delete a public member variable,
do n’t you sweat your palms? Variables are like their own lines. The variable scope is too large. If you run around without limit, then you will be worried.





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324692191&siteId=291194637