40th: Careful design method signature

Source: Internet
Author: User

1. Carefully select the name of the method. Follow the standard naming conventions.

2. Do not seek to provide a convenient method. Too many methods can make classes difficult to learn, use, document, test, and maintain.

3. Avoid too long parameter lists. The target is four parameters below.

Ways to shorten the list of parameters:

(1) Decomposition of the method into multiple methods, may lead to too many methods, through the promotion method orthogonality, you can reduce the number of methods. Java.util.List interface, which does not provide a way to find the first and last index of an element in a sub-list, this two method requires three parameters, it provides the Sublist method, requires two parameters, and IndexOf and LastIndexOf methods, requires a parameter. And the Sublist method can be combined with other methods of manipulating the list instance to perform arbitrary calculations on the sub-list, so that the resulting API has a high functional weight ratio.

(2) Create auxiliary classes, which are used to hold the grouping of parameters, usually static member classes.

(3) In the builder mode, if the method takes multiple parameters, especially if some are optional, define an object to represent all parameters and allow the client to make multiple setter calls on the object, set a parameter each time, and invoke the execution method once the required parameters are set.

4. For parameter types, interfaces are preferred instead of classes.

5. For the Boolean parameter, the enumeration type of two elements is preferred, making the code easier to read and write.

40th: Careful design method signature

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.