Rebuild notes -- remove the value assigned to the parameter, and remove the value from the rebuild.

Source: Internet
Author: User

Rebuild notes -- remove the value assigned to the parameter, and remove the value from the rebuild.

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


In the previous article, we introduced "breaking down temporary value changes". This article will introduce the refactoring method of "removing the value of a parameter.

Let's learn about This refactoring method.


Open door

Discovery: The Code assigns a value to a parameter.

Solution: Replace the parameter with a temporary variable.

// Int dicount (int inputVal, int quantity, int yearToDate) {if (inputVal> 50) inputVal-= 10 ;}
// Int dicount (final int inputVal, int quantity, int yearToDate) after reconstruction {int result = inputVal; if (result> 50) result-= 10 ;}


Motivation

I think you are very clear about the meaning of "assigning values to parameters. If you pass an object named fool as a parameter to a function, "assigning a value to a parameter" means changing fool to reference another object. However, if you perform any operations on the "passed-in object", it's okay. We often do this. Here we will only discuss the situation where "fool is changed and points to another object:

void test(Object fool){fool.changedBySomeWay(); //that's okfool=anotherObject; //trouble will appear}
We do not do this because it reduces the Definition of the code, and uses both value-based and reference-based parameter transmission methods. JAVA only uses values for passing.

When passing by value, any modification to the parameter will not affect the call end. If you only use a parameter to indicate "What is passed in", the code will be much clearer, because this usage shows the same semantics in all languages.

In JAVA, do not assign values to parameters: If you see that the code in your hand has already done so, you should use this method.



Method (1) create a temporary variable and assign the value of the parameter to be processed. (2) Use the "value assignment to a parameter" field to replace all the reference points of this parameter with "reference to this temporary variable ". (3) modify the value assignment statement to assign values to the newly created temporary variable. (4) Compile and test. (If the semantics of the Code is passed by reference, you need to check whether this parameter is used after the call. Check how many parameters passed by reference are assigned and used again. Return a value as much as possible. If there are multiple return values, you can consider changing a large amount of data to be returned to an object, or setting an independent function for each return value)

Let's start with a simple code:
int dicount(int inputVal, int quantity, int yearToDate){if(inputVal > 50) inputVal-=5;if(quantity > 100) quantity-=10;if(yearToDate > 1000) yearToDate-=100;return inputVal;}
Use a temporary variable to replace the value assignment to the parameter. The following code is obtained:
int dicount(int inputVal, int quantity, int yearToDate){int result  = inputVal;if(result > 50) result-=5;if(quantity > 100) quantity-=10;if(yearToDate > 1000) yearToDate-=100;return result;}
You can add the final keyword to a parameter and force it to follow the "No parameter assignment" Convention:
int dicount(final int inputVal, final int quantity, final int yearToDate){int result  = inputVal;if(result > 50) result-=5;if(quantity > 100) quantity-=10;if(yearToDate > 1000) yearToDate-=100;return result;}


Passing by value in JAVA

We should all know that JAVA uses a value-based function call method, which is often confusing. In all locations, JAVA follows strict pass by value:

// JAVA pass 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 uses the basic data type for parameter transmission, which is not confusing. However, if the object is passed in the parameter, the person may be confused. If the Date object is used in the program to represent the Date, the following procedures are shown:

// Class Params {public static void main (String [] args) {Date d1 = new Date (, 1); nextDateUpdate (d1); System. err. println ("d1 after nextday:" + d1); Date 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 02 00:00:00 CST 3915 d1 after nextday: tue Feb 02 00:00:00 CST 3915 arg in nextday d2: Tue Feb 02 00:00:00 CST 3915 d2 after nextday: Mon Feb 01 00:00:00 CST 3915 */
In essence, object references are passed by value. Because the internal status of the parameter object can be modified, it is meaningless to assign a value to the parameter object.

This article mainly introduces the refactoring Method -- remove the value assigned to the parameter. I think this refactoring method is not common. Generally, we don't think it is necessary to redefine the parameter value. However, to make the code clearer, it is worth doing in some cases.
Finally, I hope this article will help you. If you have any questions, please leave a message. Thank you. (PS: the next article will introduce refactoring notes-replacing functions with function objects)


Rebuild note articles as follows

Rebuild notes-getting started

Rebuilding notes-bad taste of code (I)

Rebuilding notes-bad taste of Code (Part 2)

Rebuilding notes -- Building a test body

Rebuilding notes -- refining Functions

Rebuilding notes-inline functions

Refactoring notes-inline temporary variables

Rebuilding notes -- replacing temporary variables with queries

Refactoring notes -- Introducing explanatory variables

Rebuilding notes -- breaking down temporary variables


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.