Introduce Local

The Introduce local refactor allows you to convert an expression of code to a local variable within your method (or similar). This can make your code easier to read. It is also useful if you plan to reuse the result of the expression multiple times. Figure 9.12 shows converting the discount amount (total * .10) to a local variable.

Image

FIGURE 9.12 Use Introduce Local to move code expressions into local variables.

The result of the Refactor operation is a variable to represent the discount. You can now change the code to make it more readable, as follows.

//apply discount rule
if (total > discountThreshold)
{
    var discount = total * discountPercent;
    total = total - discount;
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset