Understanding : The "use of conditional judgment instead of exception" in this article refers to the need to use the exception to judge the conditions as far as possible to change the conditions of judgment.
Detailed :
Refactoring Pre-code:
1 Public classMicrowave2 {3 PrivateImicrowavemotor Motor {Get;Set; }4 5 Public BOOLStart (ObjectFood )6 {7 BOOLfoodcooked =false;8 Try9 {Ten Motor.cook (food); Onefoodcooked =true; A } - Catch(inuseexception) - { thefoodcooked =false; - } - - returnfoodcooked; + } -}
The refactored code looks like this,TryCatchThe statement that made the conditional judgment was changed toIf return , which in many ways unifies the writing of the Code and also improves performance.
1 Public classMicrowave2 {3 PrivateImicrowavemotor Motor {Get;Set; }4 5 Public BOOLStart (ObjectFood )6 {7 if(Motor.isinuse)8 return false;9 Ten Motor.cook (food); One A return true; - } -}
This refactoring is also often used in project code because it is difficult for a part of the programmer to grasp when to useTryCatch, where should I use it?Try catch . Remember that you've talked about this before, such as how to use it well and which component should be placed in medium and large projects.
Refactoring day 18th replaces exceptions with conditional statements (replace exception with conditional)