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)