Refactoring 30th Day return as soon as possible (return ASAP)

Source: Internet
Author: User

Understanding : Simplifying the complex judgments in conditional statements by returning them as quickly as possible.

in detail: The first statement is the "decomposition of complex judgments", in a nutshell, when you have deep nesting conditions in your code, curly braces form a long arrow in your code. We often see this in different code, and this can also disrupt the readability of your code. The following code shows that the Hasaccess method contains some nested conditions, if you add some conditions or increase complexity, then the code is likely to have a few problems: 1, poor readability 2, very prone to abnormal 3, poor performance

1  Public classOrder2     {3          PublicCustomer Customer {Get;Private Set; }4 5          Public decimalCalculateorder (Customer customer, ienumerable<product> products,decimaldiscounts)6         {7Customer =customer;8             decimalOrderTotal =0m;9 Ten             if(Products. Count () >0) One             { AOrderTotal = products. Sum (p =p.price); -                 if(Discounts >0) -                 { theOrderTotal-=discounts; -                 } -             } -  +             returnorderTotal; -         } +}

Refactoring the above code is also very simple, if possible, try to remove the conditional judgment from the method, we let the code before the processing task to check the condition, if the condition is not satisfied, return as soon as possible, do not continue to execute. The following is the refactored code:

1   Public classOrder2     {3          PublicCustomer Customer {Get;Private Set; }4 5          Public decimalCalculateorder (Customer customer, ienumerable<product> products,decimaldiscounts)6         {7             if(Products. Count () = =0)8                 return 0;9 TenCustomer =customer; One             decimalOrderTotal = products. Sum (p =p.price); A  -             if(Discounts = =0) -                 returnorderTotal; the  -OrderTotal-=discounts; -  -             returnorderTotal; +         } -}

Refactoring 30th Day return as soon as possible (return ASAP)

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.