Valid Java notes (8)

Source: Internet
Author: User

List of related Reading Notes

No. 23 Check the parameter Validity

For non-public methods, we should use the assertion method to check its parameters, instead of using the check statements that are common to everyone. If the development platform we use is jdk1.4 or a more advanced platform, we can use the assert structure; otherwise, we should use a temporary asserted mechanism.

Some parameters are saved before being called during use, so you must check the parameters, otherwise, the program may throw some exceptions that make you confused (such as common NULL pointer exceptions), and you cannot immediately locate the problem. constructor is a reflection of this type, therefore, we usually check the constructor parameter validity very carefully.

In short, when writing a method or constructor, you should consider the restrictions corresponding to its parameters, and write these restrictions into the document, at the beginning of the method body, these restrictions are implemented through the displayed check.

Use protective copy when needed

Assume that the class user will do everything possible to break the constraints of the class. Under such circumstances, you must design the program in a protective manner. Class that can still be robust in the face of the customer's bad behavior.
For an immutable class, you can consider using protective copies for the variable parameters of its constructor, such
Public period (date start, date end) {<br/> This. start = new date (start. gettime (); <br/> This. end = new date (start. gettime (); <br/> // perform other logics (the protective copy is performed before other logics, And the validity check is performed on the Copied object instead of the original object) <br/>}
The get method for obtaining parameters should also be returned in clone mode, such:
Public date getstart () {<br/> return (date) Start. Clone (); <br/>}
Remember that arrays with a non-zero length are always variable. Try to use an immutable object as an internal component so that you do not have to worry about the protective copy issue.

No. 25 prototype of careful design method

1. Carefully select the method name

① It is easy to understand and consistent with other name styles in the same package;

② Select the same name as that recognized by the public;

2. Do not pursue convenient methods too much. Too many methods increase the learning and usage costs of classes. Only when an operation is frequently used can a quick addition method be provided.

3. Avoid too long parameter lists. Too long parameters are not easy for users, especially when the parameter types are the same, it is easy to produce the problem of parameter passing errors. Methods To avoid such errors:

① A method can be divided into multiple methods;

② You can create a helper class ). Organizes parameters into a class as parameters for input;

4. For type parameters, the interface is preferred, not the class. If the parameter is map, this method can receive parameters of the hashtable, hashmap, and treemap types.

5. Exercise caution when using function objects

 

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.