Refactoring notes-Removing the assignment of parameters

Source: Internet
Author: User

This article is in the study summary, welcome reprint but please specify Source: http://blog.csdn.net/pistolove/article/details/42497857


the decomposition temporary variable is described in the previous article . This article describes the refactoring technique of "removing the assignment of parameters".

Let's learn this refactoring technique.


straight to

Discovery: The code assigns a value to a parameter.

Workaround: Replace the position of the parameter with a temporary variable.

Refactor the former int dicount (int inputval, int quantity, int yeartodate) {if (Inputval >) inputval-=10;}
After refactoring int dicount (final int inputval, int quantity, int yeartodate) {int result = Inputval;if (Result >) result-=10;}


Motivation

I think you know the meaning of the words "Assigning values to parameters". If an object named Fool is passed as a parameter to a function, then assigning a parameter means changing the fool so that it references another object. However, if you're doing something on the "incoming object", that's fine, and we'll do it often. This is discussed only in the case where the fool is changed to another object:

void Test (Object fool) {Fool.changedbysomeway ();//that ' s okfool=anotherobject;//trouble'll appear}
We don't do this because it lowers the clarity of the code, and it mixes both the pass-by-value and pass-by-reference methods. Java is delivered only by value.

In the case of pass-by-value, any modification to the parameter will have no effect on the caller. If you only represent "what is passed in" as a parameter, then the code is much clearer, because this usage shows the same semantics in all languages.

In Java, you should not assign a value to a parameter: If you see the code on your hand already doing so, you should use the method in this article.



Practices(1) Create a temporary variable that assigns the value of the parameter to be processed. (2) with "Assign to parameter" as the bounds, all subsequent references to this parameter are replaced with "references to this temporary variable". (3) Modify the assignment statement so that it assigns a value to the newly created temporary variable. (4) compile, test. (if the semantics of the code are passed by reference, this parameter needs to be used after the caller checks the call.) Also check how many parameters passed by reference are assigned and then used. You should return a value as much as possible in return. If there are multiple return values, consider turning a large amount of data that needs to be returned into an object, or setting a separate function for each return value

Examplelet's start with a simple code:
int dicount (int inputval, int quantity, int yeartodate) {if (Inputval >) inputval-=5;if (Quantity > Quantity-=1) 0;if (YearToDate >) Yeartodate-=100;return inputval;}
 Replace the assignment of the parameter with a temporary variable to get the following code:
int dicount (int inputval, int quantity, int yeartodate) {int result  = inputval;if (Result >) result-=5;if (quantit Y > Quantity-=10;if (yeartodate >) yeartodate-=100;return result;}
 You can add a final keyword to a parameter, forcing it to follow the practice of "do not assign values to parameters":
int dicount (final int inputval, final int quantity, final int yeartodate) {int result  = inputval;if (Result >) res Ult-=5;if (Quantity >) quantity-=10;if (yeartodate >) yeartodate-=100;return result;}


pass-by-value for Java

We should all know that Java uses the method of function invocation by value, which often confuses everyone. In all locations, Java follows strict by-value delivery:

Java passed by value class Params{public static void Main (string[] args) {int x = 10;triple (x); System.err.println ("X after triple:" + x);} private static void triple (int arg) {arg = arg * 3; System.err.println ("Arg in Triple:" +arg);}} Output//arg in triple:30//x after triple:10
The above code is passed with the basic data type into the parameter, not confusing. But if you pass an object in a parameter, you may get confused. If a Date object is represented in the program, the following programs are shown:

The object is the parameter class Params{public static void Main (string[] args) {Date D1 = new Date (2015,1,1); nextdateupdate (D1); System.err.println ("D1 after NextDay:" + D1);D ate d2 = new Date (2015,1,1); Nextdatereplace (D2); System.err.println ("D2 after NextDay:" + D2);//61380864000000}private static void Nextdateupdate (Date d) {d.setdate ( D.getdate () +1); System.err.println ("Arg in nextday D1:" +d); private static void Nextdatereplace (Date d) {d = new Date (D.getyear (), D.getmonth (), d.getdate () +1);d =null; System.err.println ("Arg in NextDay D2:" +d);}} Output/* arg in NextDay d1:tue Feb 00:00:00 CST 3915 d1 after NextDay:   Tue Feb 00:00:00 CST 3915 Arg in NextDay D2:  Tue Feb 00:00:00 CST 3915 D2 after NextDay:   Mon Feb 00:00:00 CST 3915 */
 In essence, a reference to an object is passed by value. Because the internal state of the parameter object can be modified, there is no point in re-assigning a value to the Parameter object.

This paper mainly introduces the reconstruction technique--removing the assignment of parameters . I think this refactoring technique is not very common, and in general we feel that it is not necessary to redefine the value of the parameter. But to make the code look clearer, it's worth doing in some cases.
Finally, I hope this article will be of some help to you. There are questions to leave a message, thank you. (PS: The next article will introduce refactoring notes--replacing functions with function objects)


The refactoring Note article is as follows

Refactoring Notes-Introductory article

Refactoring notes-bad taste of code (UP)

Refactoring notes-bad taste of code (bottom)

Refactoring notes-building a test body

Refactoring notes-Refining functions

Refactoring notes-inline functions

Refactoring notes-Inline temporary variables

Refactoring notes-replacing temporary variables with queries

Refactoring notes-Introduction of explanatory variables

Refactoring notes-breaking temporary variables


Refactoring notes-Removing the assignment of parameters

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.