10th Day of Refactoring: extracting Methods (Extract method)

Source: Internet
Author: User

understand : Often write code, there are some computational logic more complex methods, write down a very long and long method, we can put this method, according to the function, decomposition into a few small methods alone. This will not only increase the maintainability of the code, but also increase legibility.

Detailed :

Refactoring Pre-code:

1  Public classReceipt2     {3         Privateilist<decimal> Discounts {Get;Set; }4         Privateilist<decimal> itemtotals {Get;Set; }5 6          Public decimalcalculategrandtotal ()7         {8             decimalSubTotal =0m;9             foreach(decimalItemtotalinchitemtotals)TenSubTotal + =itemtotal; One  A             if(Discounts.count >0) -             { -                 foreach(decimalDiscountinchdiscounts) theSubTotal-=discount; -             } -  -             decimalTax = SubTotal *0.065m; +  -SubTotal + =Tax ; +  A             returnsubTotal; at         } -}

We see the Calculategrandtotal method, which uses 2 foreach loops, to see, it is really inappropriate to understand, do not know what each foreach mainly do.

Look at the refactoring code:

1  Public classReceipt2     {3         Privateilist<decimal> Discounts {Get;Set; }4         Privateilist<decimal> itemtotals {Get;Set; }5 6          Public decimalcalculategrandtotal ()7         {8             decimalSubTotal =calculatesubtotal ();9 TenSubTotal =calculatediscounts (subTotal); One  ASubTotal =Calculatetax (subTotal); -  -             returnsubTotal; the         } -  -         Private decimalCalculatetax (decimalsubTotal) -         { +             decimalTax = SubTotal *0.065m; -  +SubTotal + =Tax ; A             returnsubTotal; at         } -  -         Private decimalCalculatediscounts (decimalsubTotal) -         { -             if(Discounts.count >0) -             { in                 foreach(decimalDiscountinchdiscounts) -SubTotal-=discount; to             } +             returnsubTotal; -         } the  *         Private decimalcalculatesubtotal () $         {Panax Notoginseng             decimalSubTotal =0m; -             foreach(decimalItemtotalinchitemtotals) theSubTotal + =itemtotal; +             returnsubTotal; A         } the}

After refactoring, we decompose the Calculategrandtotal method into 3 small methods, each named according to its function. Calculatetax as the name implies, is to calculate tax, calculatediscounts is calculate discount amount.

This makes it clear that the logic is stripped away.

10th Day of Refactoring: extracting Methods (Extract method)

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.