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