Dahua design Pattern (two code specification, refactoring)
(Continuation of the article)
Big Bird said: "And do not say the topic of the person's meaning, alone in your current code, there are many deficiencies need to improve." Like variable naming, your name is ABCD, the variable does not have any concrete meaning, this is very irregular; Judging branch, you're writing like this, meaning that every condition has to be judged, equal to three times the computer did no work; Data entry validity judgments, etc., if the user entered a character symbol instead of a number. If the divisor, the customer entered 0 what to do. These are areas that can be improved. ”
"Oh, that's right, I have heard the teacher said before, but never cared about, I immediately change, change the end to show you." ”
Class Program {static void Main (string[] args) {try {console.write ("Please enter a number a:");
String strnumbera = Console.ReadLine ();
Console.Write ("Please select the operator symbol (+ 、-、 *,/):");
String stroperate = Console.ReadLine ();
Console.Write ("Please enter the number B:");
String strnumberb = Console.ReadLine ();
String strresult = ""; Switch (stroperate) {case "+": strresult = convert.tostring (Convert.todou
BLE (Strnumbera) + convert.todouble (strnumberb));
Break
Case "-": Strresult = convert.tostring (convert.todouble (Strnumbera)-convert.todouble (Strnumberb));
Break
Case "*": Strresult = convert.tostring (convert.todouble (Strnumbera) * convert.todouble (Strnumberb));
Break Case "/": if (Strnumberb!= "0") strresult = Convert.ToString (convert.todouble (Strnumbera)/convert.todouble (Strnumberb));
else strresult = "Divisor cannot be 0";
Break
} Console.WriteLine ("The result is:" + strresult);
Console.ReadLine (); The catch (Exception ex) {Console.WriteLine ("Your input is wrong:" + ex.)
message); }
}
}
Big Bird: "Ho ho, yes, yes, change quickly." To the current code, the implementation of the calculator is no problem, but this write code is the meaning of the author. ”
Side dishes: "You mean object oriented." ”
Big Bird: "Ha, the vegetable is not the vegetable also." ”
adjourned
Source: http://www.cnblogs.com/cj723/archive/2006/08/30/490094.html