Effective Java Reading notes six methods

Source: Internet
Author: User

First, check the validity of the parameters

1, consider the limitations of the parameters, write the restrictions to the document, at the beginning of the method by explicit check to enforce these restrictions.

Second, if necessary, a protective copy

1. If a class has a mutable component that is obtained or returned from the client, the class must consider a protective copy of these components.

2, if the copy cost is high, the class can also trust his client, do not copy protection, but to document the description.

Third, careful design method signature

1, carefully choose the name of the method: abide by the norms, to maintain the same style.

2, do not seek to provide convenient methods.

3. Long-coded parameter list

A, a method decomposed into multiple methods

B, create auxiliary classes, save the grouping of parameters.

C. Using the builder mode

4. For parameter types, use interfaces rather than classes to prioritize

5. For the Boolean parameter, use the enumeration type of the two element first.

Iv. using heavy loads with caution

1. Calls to overloaded methods are made at compile time. The choice of the override method is to make a decision at run time.

2. Try not to export multiple overloaded methods with the same number of parameters.

3. Do not overload a method with variable parameters

Five, cautious use of variable parameters

Returns an array or collection of 0 lengths, not NULL

1. Methods that return types to arrays or collections should return a 0-length array or collection instead of NULL.

Vii. Writing documentation comments for all exported API elements

  

  

Effective Java Reading notes six methods

Related Article

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.