I am a programmer. Believe that many programmers have the same experience, in the company to scold others code rotten, while writing a worse than others code.
The code is not bad from the start, the code is bad there are the following possible
1. Change of requirements
2. The level of people who maintain the code is uneven
3. Large flow of maintenance personnel
4. Short development cycle, emergency delivery time
5. Personal habits
6, the manager of the code requirements
All of these are possible to make the code bad, then how to write good code? In addition to personal programming skills need to have a certain skill, the following is to summarize the previous experience
Come on, need to develop a good habit in programming, let write good code become a habit BAR!!!!!
1, use the Guardian statement
Under what circumstances can you use the Guardian statement, as follows
public void Add (Object element) {if (!readonly) {int newSize = size + 1;if (NewSize > Elements.length) {object[] Newele ment = new Object[elements.length + 10];for (int i = 0; i < size; i++) {newelements[i] = elements[i];} elements = newelements;} elements[size++] = element;}}
After using the Guardian statement refactoring, the following
public void Add (Object element) {if (readOnly) return;int newSize = size + 1;if (NewSize > Elements.length) {object[] ne Welement = new Object[elements.length + 10];for (int i = 0; i < size; i++) {newelements[i] = elements[i];} elements = newelements;} elements[size++] = element;}
This makes nesting less of a layer, reducing complexity
2. Refining method
When you see a function that is too long or a piece of code that needs to be annotated to make sense, you should put this code in a separate function, which is the most common refactoring technique.
One, so under what circumstances can the method be refined?
One, duplicate code
Second, the use of assistant method, is to extract the annotation part into a function, as follows
Public List getusers () {List users = new arraylist<user> ();//.........//sort by most recently registered Userscollec Tions.sort (Users, New User.usercomparatorbydescendingregistration ()); return users;}
After refining the method
Public List getusers () {List users = new arraylist<user> ();//.........sortbymostrecentlyregistered (users); return users;}
Three, the function structure of a single level, as follows
Before refactoring
void printowing (double amount) { printbanner (); System.out.println ("Name:" + _name); System.out.println ("Amount" + amount); }
After refactoring
void printowing (double amount) { printbanner (); Printdetails (amount); }
Iv. Replace the temporary variable with a query, and if you replace the temporary variable with a query, all functions in the same class can obtain this information;
Refactoring pre-code
Public double GetPrice () { double baseprice = _quantity*_itemprice; if (Baseprice >) { return baseprice * 0.95; } else{ return baseprice * 0.98; } }
Post-Refactoring Code
Public double GetPrice () { if (Baseprice () >) { return Baseprice () * 0.95; } else{ return Baseprice () * 0.98; } private int Baseprice () { return _quantity* _itemprice; }
3, the role of a single variable, the following code of the AVG has two functions, meaning is not clear, easy to create the Qi Yi
Private double Calculateaverage () {Iterator grades = Clines.iterator ();d ouble avg = 0.0;while (Grades.hasnext ()) {grade = ( Coursegrade) Grades.next (); Grade.grade = = "F")) {avg = avg + grade.grade-64;}} Avg = Avg/clines.size (); return avg;}
4, reorganize the code, the variable definition and use of the span is not too large, that is, in the use of re-defined, easier to understand
Refactoring pre-code
Marketing_data *marketingdata = new Marketing_data; Sales_data *salesdata = new Sales_data; Travel_data *traveldata = new Travel_data; Traveldata.computequarterly (); Salesdata.computequarterly (); Marketingdata.computequarterly (); Salesdata.computeannual (); Marketingdata.computeannual (); Traveldata.computeannual (); Salesdata.print (); Traveldata.print (); Marketingdata.print ();
Post-Refactoring Code
Marketing_data *marketingdata = new Marketing_data; Marketingdata.computequarterly (); Marketingdata.computeannual (); Sales_data *salesdata = new Sales_data; Salesdata.computequarterly (); Salesdata.computeannual (); Salesdata.print (); Travel_data *traveldata = new Travel_data; Traveldata.computequarterly (); Traveldata.computeannual (); Traveldata.print ();
Resources:
1, http://www.cnblogs.com/matchcolor/archive/2010/08/02/1784888.html
Code Refactoring Tips Method