Reconstruction Method-Simplified function call [1], reconstruction method-Simplified Function

Source: Internet
Author: User

Reconstruction Method-Simplified function call [1], reconstruction method-Simplified Function

Total returned directory

Directory of this section

  • Rename Method (renamed function)
  • Add Parameter (Add Parameter)
  • Remove Parameter (Remove Parameter)
1 Rename Method (function Rename) Summary

The function name does not reveal the function's purpose.

Modify the name of a function.

Motivation

It is a good programming style to break down complex processing processes into small functions. However, if you do not do well, you will have to take a long time to understand the respective uses of these small functions. To avoid these troubles, the key is to give a good name to the function. The function name should accurately express its purpose. There is a good way to name a function: first consider writing a comment to this function, and then try to convert the comment into the function name.

If you see that a function name cannot express its purpose, you should modify it immediately.

Example

As shown in the following code, if a company creates an employee class, the class contains a employee name field and a method for calculating employee income by hour, the name of the code below is difficult to understand, so we will refactor the name.

public class Person{    public string FN { get; set; }    public decimal ClcHrlyPR()    {        // code to calculate hourly payrate        return 0m;    }}

The code after reconstruction is as follows, which looks very clear. If there are new members of the project team, they will become very happy to read the code.

public class Employee{    public string FirstName { get; set; }    public decimal CalculateHourlyPay()    {        // code to calculate hourly payrate        return 0m;    }}
Summary

This refactoring is often ignored by the majority of programmers, but the hidden danger is immeasurable. Maybe the boss wants to modify the function. Let's look at this section of code without refactoring (even if it is written by myself, however, due to the relationship between the time and the project, it is hard to understand), and then it will become overwhelmed. On the contrary, the reconstructed code will be clear and pleasing to the eye.

Of course, the name in this article is not only a function, but also a class, method parameter, variable, Delegate, event, and other elements.

2Add Parameter (add Parameter) Summary if a function needs to obtain more information from the caller Add a parameter to the function to bring the required information into the function.Motivation

This refactoring method is often used. I believe you have used it.

You must modify a function. The modified function requires information that is not available in the past. Therefore, you must add a parameter to the function.

So when will this item be used for reconstruction?

Except Add Parameter, other options are better than "Add Parameter" if possible, because other options may not increase the length of the Parameter column. Too long parameter columns are bad because it is difficult for programmers to remember so many parameters and often accompanied by bad taste Data Clumps.

3 Remove Parameter (Remove Parameter) Summary

The function body no longer needs a parameter,Remove this parameter.

Motivation

Programmers may often add parameters but are reluctant to remove them. They thought: in any case, redundant parameters won't cause any problems and may be used later.

This idea is terrible! Parameters represent the information required by the function, and different parameters represent different meanings. Function Callers must worry about what to upload for each parameter. If you do not remove unnecessary parameters, you will be charged for each user.

 

To Be Continued ......

Related Article

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.