Reconstruction Method 48: preserve whole object (keep object complete)

Source: Internet
Author: User

You can retrieve several values from an object and use them as parameters for calling a callback function.To pass the entire object.

Motivation: Sometimes you pass several items from the same object as parameters to a function. In this case, if a new data item is required for a function to be called in the future, you must search for and modify all calls to the function. If you pass the entire object to which the data belongs to the function, you can avoid this embarrassing situation, because the called function can request any information it wants from that parameter object.

In addition to making the parameter columns more stable, the preserve whole object (keep the object complete) can also improveCodeReadability. Too long parameter columns are difficult to use, because callers and callers must remember the purpose of these parameters. In addition, failing to use a complete object will cause repeated code, because the called function cannot use the function in the complete object to calculate some intermediate values.

However, there are always two sides: If you pass a value, the called function only depends on these values, rather than the objects to which they belong. However, if you pass the entire object, the object where the called function is located must depend on the parameter object. If this deteriorates your dependency structure, you should not use preserve whole object (keep the object complete ).

Some people think that if the called function only needs a value of the parameter object, it would be better to pass only that value. This idea cannot be recognized because passing a value and passing an object are at least consistent in Code definition. More important considerations should be placed on the dependency between objects.

If the called function uses many data items from another object, this may mean that the function should actually be defined in the objects to which the data belongs. Therefore, consider using preserve whole object (to keep the object complete), and you should also consider moving method (the moving function ).

Before using this item for reconstruction, you may not have defined a complete object, so you should first use the introduce parameter object (introduce the parameter object ).

Another common case is that the caller uses several values as parameters and passes them to the called function. In this case, if the object has an appropriate value function, you can use this to replace these parameter values without worrying about object dependencies.

Practice: 1. Add a new parameter to your target function to represent the complete object of the original data.

2, Compilation, and testing.

3To determine which parameters can be included in the newly added complete object.

4Select one of the above parameters, and change the original reference of this parameter in the called function to the corresponding value function of the newly added parameter object.

5, Delete this parameter.

6, Compilation, and testing.

7. Repeat the above process for all parameters that can be obtained from the complete object.

8Delete the Code with the deleted parameters in the call end.

9, Compilation, and testing.

 

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.