Alibaba-java Development Handbook Experience-a programming protocol -4oop (object-oriented programming) protocol

Source: Internet
Author: User

1. "Forcing" avoids accessing static or static methods of this class through the object reference of a class, unnecessarily increasing the cost of compiler parsing and accessing it directly with the class name.

2. "Mandatory" all the overwrite method , must add @override annotation.

Description: A problem with GetObject () and Get0bject (). One is the letter O, one is the number of 0, plus @override can accurately determine whether the coverage is successful. In addition, if the method signature is modified in an abstract class, the implementation class compiles the error immediately.

3. "Force" the same parameter type, the same business meaning, you can use the variable parameters of Java, avoid using object.

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

Positive example: Public User getusers (String type, Integer ... ids) {...}
4. "Force" an interface that is being invoked externally or that is dependent on a two-party library, does not allow modification of the method signature , and avoids having an impact on the interface caller.

interfaces that are obsolete must be annotated with @deprecated and clearly describe what the new interface or new service is used for.

5. "Enforce" cannot use obsolete classes or methods .

Description: Method Decode (String encodestr) in Java.net.URLDecoder is obsolete and should use a dual-parameter decode (string source, string encode).

Since the interface provider is explicitly outdated, it is incumbent on the new interface to be available at the same time, and as a call to Towners says it is incumbent upon the new implementation of the outdated method to be verified.

6. The equals method of the "Force" object is apt to throw a null pointer exception, and the equals should be called with a constant or a value-determining object.

Positive example: "Test". Equals (object);

Inverse example: Object.Equals ("test"); Description: Recommended use of java.util.objects#equals (tool class introduced by JDK7)

7. "Force" all comparisons between the values of the same type of wrapper class object , all using the Equals method.

Description: for integer var =? In the range of 128 to 127 assignment, the integer object is generated in the Integercache.cache, the existing objects will be reused, the integer value within this interval can be directly used = = to judge,

However, all data outside this interval will be generated on the heap and will not be reused for existing objects, which is a big pit, and it is recommended to use the Equals method for judgment.

8. The standard of use for basic data types and packaging data types is as follows:

1) "Force" all the Pojo class properties must use the wrapper data type.

2) The return value and parameters of the "force" RPC Remote Procedure Call protocol method must use the wrapper data type.

3) "Recommended" all local variables use the base data type.

Description: The Pojo class attribute does not have an initial value that reminds the user to make an explicit assignment when needed, and any NPE (null pointer exception) problem, or inbound check, is guaranteed by the user.

A positive example: The query result of a database may be null because it is automatically disassembled , and the risk of NPE is received with the base data type.

Counter example: For example, show the total turnover of the situation, that is, positive and negative x%,x for the basic data type, call the RPC service, when the call is unsuccessful, the return is the default value, page display: 0%,

' This is unreasonable and should be shown as a medium dash. Therefore, a null value of the wrapper data type can represent additional information, such as: A remote call failed and an exception exited.

9. Do not set any property defaults when "Force" defines a Pojo class such as Do/dto/vo.

Counter Example: The Gmtcreate default value for the Pojo class is new Date ();

However, this property does not place a specific value in the extract, and updates the field with the update of the other fields, causing the creation time to be modified to the current time.

10. When the "force" serialization class adds a new attribute, do not modify the Serialversionuid field to avoid deserialization failure;

If you are completely incompatible with the upgrade and avoid deserialization, modify the Serialversionuid value. Description: Note The Serialversionuid inconsistency throws a serialized run-time exception.
11. The "forced" construction method prohibits any business logic from being added, and if there is initialization logic, put it in the Init method.
12. The "force" Pojo class must write the ToString method. When using the IDE's tools:source> generate ToString, if you inherit another Pojo class, be careful to add super.tostring to the front. Description: When the method execution throws an exception, you can directly call Pojo's ToString () method to print its property values for easy troubleshooting.
13. "Recommended" when using an index to access an array obtained using the split method of string, there is no content check after the last delimiter, otherwise there is a risk of throwing indexoutofboundsexception.
Description
String str = "a,b,c,,";
string[] ary = Str.split (",");
Expected to be greater than 3, the result is 3
System.out.println (ary.length);
14. "Recommended" When a class has multiple constructor methods, or multiple methods with the same name, these methods should be placed together in order for readability.
15. The "recommended" in-class method definition order is: Public method or Protection method > Private Method > Getter/setter method.

Alibaba-java Development Handbook Experience-a programming protocol -4oop (object-oriented programming) protocol

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.