System refactoring requires that every step of code modification cannot change the external behavior of the software. Therefore, all methods in system refactoring are equivalent changes of code. The reconstruction process is like performing an equivalent transformation of the formula step by step in a mathematical problem. After a series of equal transformation, although the final result is different from the original form, it can be obtained through computation.
This equivalent transformation is very important for refactoring. It makes the program, the code, and the code after reconstruction. However, the equivalent transformation is not equal to the original step. Just as the matrix can get the solution of the equations through the equivalent transformation, calculus can calculate the final result through the equivalent transformation, refactoring through the equivalent transformation, while ensuring the correct code, can optimize the program structure. To illustrate this equivalent transformation in system refactoring, let's take a look at a simple example. The original program is like this:
1 public class HelloWorld { 2 public String sayHello(Date now, String user){ 3 Calendar c; 4 int h; 5 String s = null; 6 c = Calendar.getInstance(); 7 c.setTime(now); 8 h = c.get(Calendar.HOUR_OF_DAY); 9 if(h>=6 && h<12){10 s = "Good morning!";11 }else if(h>=12 && h<19){12 s = "Good afternoon!";13 }else{14 s = "Good night!";15 }16 s = "Hi, "+user+". "+s;17 return s;18 }19 }
This is a very simple helloworld program, which is written to make it easier for everyone to understand the transformation process of the program. Although simple, this program conforms to many features of the legacy system: no comments, sequential programming, no layers, low degree of polymerization, and so on. Therefore, we made initial refactoring, adding annotations, adjusting the order, renaming variables, and performing segmentation:
1 /** 2 * The Refactoring‘s hello-world program 3 * @author fangang 4 */ 5 public class HelloWorld { 6 /** 7 * Say hello to everyone 8 * @param now 9 * @param user10 * @return the words what to say11 */12 public String sayHello(Date now, String user){13 //Get current hour of day14 Calendar calendar = Calendar.getInstance();15 calendar.setTime(now);16 int hour = calendar.get(Calendar.HOUR_OF_DAY);17 18 //Get the right words to say hello19 String words = null;20 if(hour>=6 && hour<12){21 words = "Good morning!";22 }else if(hour>=12 && hour<19){23 words = "Good afternoon!";24 }else{25 words = "Good night!";26 }27 words = "Hi, "+user+". "+words;28 return words;29 }30 }
Then extract the codes in the two comments to form the gethour () and getsecondgreeting () functions:
1 /** 2 * The Refactoring‘s hello-world program 3 * @author fangang 4 */ 5 public class HelloWorld { 6 /** 7 * Say hello to everyone 8 * @param now 9 * @param user10 * @return the words what to say11 */12 public String sayHello(Date now, String user){13 int hour = getHour(now);14 return "Hi, "+user+". "+getSecondGreeting(hour);15 }16 17 /**18 * Get current hour of day.19 * @param now20 * @return current hour of day21 */22 private int getHour(Date now){23 Calendar calendar = Calendar.getInstance();24 calendar.setTime(now);25 return calendar.get(Calendar.HOUR_OF_DAY);26 }27 28 /**29 * Get the second greeting.30 * @param hour31 * @return the second greeting32 */33 private String getSecondGreeting(int hour){34 if(hour>=6 && hour<12){35 return "Good morning!";36 }else if(hour>=12 && hour<19){37 return "Good afternoon!";38 }else{39 return "Good night!";40 }41 }42 }
In this example, we can see that adjusting the write order of statements without sequential order is an equivalent transformation, which extracts a relatively independent statement from a specific segment of the statement to form a function, calling this function with the original statement is also an equivalent transformation. In addition, adjusting the function name, modifying the variable name, and so on are all equivalent transformations. Equivalent transformations, programs, and execution results, but the organizational structure of the program has changed and become more readable, maintainable, and easy to change. This is the meaning of refactoring.
The code of a program is divided into several functions by function, which can effectively improve the readability of the Code. Various variables and functions in the program are reasonably named, commenting in the function header or definition at the right time is also improving the readability of the Code; properly assigning a wide variety of functions to their respective objects, this is to effectively improve the maintainability and ease of change of the system. These are very helpful for the daily maintenance and lifecycle of a legacy system.
Big talk rebuilding serialization home: http://www.cnblogs.com/mooodo/p/talkAboutRefactoringHome.html
Note: I hope that the author or source should be noted to show my respect for the author when I reprint this article. Thank you!