List of related Reading Notes
No. 27 returns an array of zero length instead of null.
If null is returned, null judgment is required for every call to this method. Otherwise, a null pointer exception is easily thrown. We recommend that you return a zero-length array. In general, this method has almost no impact on performance.
Article comment for all exported API Elements
To add comments: Class, interface, constructor, method, and domain declaration. The content of method comments:
Prerequisites for calling this method;
Subsequent processing after the call (for example, capturing exceptions );
Side effects (such as the security after the method starts the thread );
Parameter @ Param describe;
Return @ return describe;
Exception @ throws if .....;
Note: HTML tags such as <p> <code> <tt> can be used in annotations, but tags such as >,< must be escaped.
No. 29 minimize the scope of local variables
Declare the local variable where the local variable is applied for the first time;
It is necessary to prevent local variables from being declared out of "using its blocks", which will prevent the local variables from being accidentally used;
Initialization is required for almost every local variable. If there is not enough information to initialize a variable in a meaningful way, the declaration will be postponed until it can be initialized;
Other methods, such as dividing a variable into two methods, operate one method at a time to reduce interference between variables.
No. 30 understand and use the database
Do not create a wheel from scratch. If you want to do something very common, check whether there is such an implementation class. If so, use it, this will reduce your investment in implementing the corresponding functions and code error rate.
No. 31 avoid using float and double if exact answers are required
Float and double are not suitable for currency. They should be avoided during normal usage. If you want the system to process decimal places, you can use bigdocimal. If you do not consider decimal places, if the value range is no more than 9 bits, int can be used for processing. If the value range is no more than 18 bits, long can be used for processing. If the value range is greater than 18 bits, bigdecimal must be used for processing.
No. 32 avoid using strings if other types are more suitable
If you can use a more suitable data type or write a more appropriate data type (such as do, pojo, enumeration, etc.), you should avoid using strings to represent objects, strings are more clumsy and less flexible than other types.
No. 33 understand the performance of string connection
To connect n strings and use the "+" connection repeatedly, it takes n square-level time. Use stringbuffer instead of string for higher performance.
No. 34 reference objects through interfaces
The interface rather than the class should be used to reference objects. If an appropriate interface exists, the interface type should be used for parameter return values, variables, and field declarations, for example, if vector is the implementation of the list interface, the declaration should be as follows:
List subscribers = new vector (); <br/> // instead of <br/> vector subscribers = new vector ();
Exceptions:
① If no suitable interface exists, you can use a class instead of an interface to reference an object, such as string or integer;
② When an object is a basic type class, rather than an interface, it should be referenced with the relevant base class, such as Java. util. timertask;
③ When a class implements an interface, it provides additional methods that do not exist in the interface. If the program depends on these additional methods, such a class should only be used to reference its instance, and should never be used as a parameter type.
No. 35 interface takes precedence over image mechanism (reflection mechanism)
The reflection mechanism is a powerful function of Java. It is necessary for some specific complicated programming (such as the Popular Spring framework), but when the reflection mechanism is not necessary, avoid reflection as much as possible for the following reasons:
① It does not perform type check during compilation;
② The implementation code is tedious and not easy to read;
③ The performance is much lower than that of common method calls;
If a program must work with an unknown class at the time of compilation, it is best to use reflection to instantiate the object, and access the object using an interface or parent class known at the time of compilation.
No. 36 use the local method with caution
Try to use the methods provided by Java to replace local methods (for example, use the new functions provided by Java to replace the functions previously only implemented by C language). This will make the system more secure, the system has higher portability and makes the code easier to read. If you must use a local method, enhance the test and use it as little as possible.
No. 37 careful Optimization
If you try to write a program that is not a fast program, the program should embody the principle of information hiding. The performance problem should be considered at the design stage, and the design decisions that limit the performance should be avoided, such: public classes in the composite mode should be used for inheritance, so the performance of this class is permanently affected by the performance of its parent class. It is not a good practice to modify the API for good performance. Generally, these practices have little impact on performance. If a system has a clear, concise, and well-structured implementation, please be careful when optimizing it, because 80% of the performance problems exist in 20% of the Code, finding out the code that affects the performance is the key to the problem. You can use some performance analysis tools.