Step by step reconstruction study notes four temporary variables camp .NET code (Temporary Variable)

Split Temporary Variable (anatomy temporary variables)

Outline

The program has a temporary variable is assigned more than once, it is neither a loop variable, not a set of temporary variables (collecting temporary variable)

Motivation (Motivation)

Temporary variables serve various purposes, some of which use will naturally lead to the temporary variable is assigned a plurality of times (the loop variable) and (set temporary variables) are two typical examples: loop variable (loop variable) will vary circulating each run varied (e.g. for (int i = 0; i <10; i ++)) statement I); set a temporary variable (collecting temporary variable) is responsible (by calculation for the whole function) constituted a value is collected.

Practice (Mechanics)

1, in the declarative (to be dissected) the temporary variable is assigned its first office, modify its name.

If the assignment is later i = j is a form of expression, it means that this is a temporary variable set, then do not dissect it. Set temporary variable effect is generally additive, the string engaging, write or add elements to the cluster stream (collection).

2, assigned to the second movement of the temporary variable for the sector, after modifying the point of reference for all temporary variables so that they reference the new temporary variable.

3, in the second assignment, the original re-declare the temporary variable.

Examples

        public void GetArea(double _height, double _width)
        {
            double temp = 2 * (_height + _width);
            Console.WriteLine(temp);
            temp = _width * _height;
            Console.WriteLine(temp);
        }

Read:

        public void GetArea(double _height, double _width)
        {
            double temp = 2 * (_height + _width);
            Console.WriteLine(temp);
            double area = _width * _height;
            Console.WriteLine(area);
        }

Remove Assignments to Parameters

Outline

A code parameter assignment actions.

Motivation (Motivation)

Choice, we make sure we all know (parameter assignment) The phrase means. If you put an object named foo as a parameter to a function, then (parameter assignment) means to change foo,

It makes reference to another object.

Practice (Mechanics)

1, create a temporary variable, the parameter values ​​to be processed to give it.

2, (the assignment to the parameters for the sector, all subsequent references point to this parameter, replace all references to action (this temporary variables).

3, modify the assignments for language name, it was changed to the new temporary variable assignment.
Examples

        public int Discount(int inputVal, int quantity, int yeaarToDate)
        {
            if (inputVal > 50) inputVal -= 2;
            if (quantity > 100) inputVal -= 1;
            if (yeaarToDate > 10000) inputVal -= 4;
            return inputVal;
        }

 

Read:

        public int Discount(int inputVal, int quantity, int yeaarToDate)
        {
            int result = inputVal;
            if (inputVal > 50) result -= 2;
            if (quantity > 100) result -= 1;
            if (yeaarToDate > 10000) result -= 4;
            return result;
        }

 

to sum up

Try not to operate on the parameters to a temporary variable to replace it.

Reproduced in: https: //www.cnblogs.com/springyangwc/archive/2011/05/18/2050514.html

Guess you like

Origin blog.csdn.net/weixin_34085658/article/details/93340909