Reconstruction Method 49: replace parameter with methods (replace parameters with functions)

Source: Internet
Author: User

The object calls a function and passes the result as a parameter to another function. The function that accepts this parameter can also call the previous function.Let the parameter receiver remove this parameter and directly call the previous function.

Motivation: If a function can obtain the parameter value through other means, it should not obtain the value through the parameter. Too long parameter columns will increaseProgramReaders are difficult to understand. Therefore, the length of parameter columns should be shortened as much as possible.

One way to reduce the parameter column is to check whether the parameter receiver can obtain the parameter value through the same calculation as the caller. If the caller computes parameters through another function in the object to which the caller belongs, and does not reference other parameters in the call, the calculation process should be transferred to the called end, this parameter is removed. If the called function belongs to another object, and the object has reference to the object of the call end, the above also applies.

However, if the calculation process of the parameter value depends on a parameter on the call end, the parameter of the called end cannot be removed because the parameter value may be different during each call. In addition, if the parameter receiver does not have a reference to the parameter sender object and you do not want to add such a reference, the parameter cannot be removed.

Sometimes, parameters exist for future flexibility. In this case, you can still remove this extra parameter. You should add parameters only when necessary. The pre-added parameters are probably not what you need. There is an exception to this rule: if modifying an interface will cause a very painful result to the entire program, you can consider retaining the parameters previously added. If this is the case, you should first determine the serious consequences of modifying the interface, and then consider whether to reduce the dependency between parts to reduce the impact of modifying the interface. Stable interfaces are good, but freezing them on a bad interface is also a problem.

Practice: 1. Extract the calculation process of parameters into an independent function if necessary.

2, Change the place in the function body that references this parameter to call the new function.

3, After each replacement, compile and test.

4After all parameters are replaced, use remove parameter to remove the parameter.

 

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.