Refactoring of the "refactoring learning" 09 function call

Source: Internet
Author: User

Before I wrote a refactoring of a function, it was written about the refactoring of the call of the function, which is different, in order to write a function ( or interface ) that makes it better to call.

1. Function renaming

Modify point: The name of the function does not prompt the function's purpose.

Procedure: Modify the name of a function

If you want to write a comment on a function, then you can write the comment as a name.

Martin's exact words:

You may not be able to get a good name for the first time, this time you will think, just do it, after all, it is just a name.

Beware, this is the call of the devil, the Path to chaos, and never be tempted by it.

( I've been tempted countless times, and I've taken a lot of dregs, because it's so hard to think of a good name, unless I write a long name for the function)

2. Adding parameters

Modify point: A function needs to get more information from the caller

Practice: Add an object parameter to this function to bring the object into all the information in the function.

3. Remove Parameters ( well, compared to the 2nd, a lot of people might be bothered not to engage in this, Demon temptation Oh )

Modify point: The function body no longer requires a parameter

Practice: Remove this parameter

4, the query function and modify function separation ( so directly with the property is good AH )

Modify point: A function returns both the object state value and the object state

Practice: Establish two different functions, one of which is responsible for querying, and is responsible for modifying

There are exceptions to this, which is the simultaneous query and modification of the concurrency scenario, so you should still separate, but write a function to do these two things at the same time.

5. Make the function carry parameters

Modify points: Several functions do similar work, but contain different values in the function body

Practice: Create a single function that expresses those different values with parameters

Simply put, two functions have a lot of the same parts, just a few values are different, you take these values as function parameters, then you can put two functions into one

6. Replace the parameter with a definite function ( as opposed to 5 oh )

Modify point: You have a function which depends entirely on the value of the parameter and takes different behavior ( bullish is complete )

Practice: Establish a separate function for each possible value of this parameter

It means that you take different actions based on the judgment of the parameters, so you can do it in several functions.

And if the impact is not very large, with 5 is good, if you do need conditional judgment, then you can consider using polymorphism to eliminate the conditional judgment

7. Keep the object intact

Modify point: You remove several values from an object and use them as arguments for a function call

Practice: Pass the entire object instead

Motive: In case future functions require new data items, you must find and modify all calls to this function. This can also improve the readability of the entire code.

However, there are exceptions: if you are wearing a numeric value, then the function depends only on the value, but if it is the object, then the whole object is dependent on it, which may cause deterioration of your structure, so you have to make specific analysis of the situation.

But if a function uses a lot of values from another object, you might want to consider whether you need to put the function in the class that the object belongs to.

Refactoring of the "refactoring learning" 09 function call

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.