重构技巧
重新组织你的函数
在对象之间搬移特性
重新组织数据
简化条件表达式
简化函数调用
处理概括关系
提炼函数1
1: String name = request.getParameter("Name");
2: if( name != null && name.length() > 0 ){
3: ......
4: }
5:
6: String age = request.getParameter("Age");
7: if( age != null && age.length() > 0 ){
8: ......
9: }
转化后:
1: String name = request.getParameter("Name");
2: if( !isNullOrEmpty( name ) ){
3: ......
4: }
5: String age = request.getParameter("Age");
6: if( !isNullOrEmpty( age ) ){
7: ......
8: }
9:
10:
11: private boolean isNullOrEmpty( final String string ){
12: if( string != null && string.length() > 0 ){
13: return true;
14: }else{
15: return false;
16: }
17: }
提炼函数2:清除注释
1: void printOwing() {
2: //print banner
3: System.out.println(“*********”);
4: System.out.println(“Banner”);
5: System.out.println(“*********”);
6: //print details
7: System.out.println ("name: " + _name);
8: System.out.println ("amount " +
9: getOutstanding());
10: }
转化后:
1: void printOwing(){
2: printBanner();
3: printDetails(getOutstanding());
4: }
5:
6: void printBanner(){
7: System.out.println(“*********”);
8: System.out.println(“Banner”);
9: System.out.println(“*********”);
10: }
11:
12: void printDetails (double outstanding){
13: System.out.println ("name: " + _name);
14: System.out.println ("amount " + outstanding);
15: }
将临时变量内联化:
1: int i = getNo();
2: if ( i > 100)
转化后:
1: if ( getNo() > 100)
以查询取代临时变量:
1: double basePrice = _quantity * _itemPrice;
2: if (basePrice > 1000)
3: return basePrice * 0.95;
4: else
5: return basePrice * 0.98;
转化后:
1: if (basePrice() > 1000)
2: return basePrice() * 0.95;
3: else
4: return basePrice() * 0.98;
5: ...
6:
7: double basePrice() {
8: return _quantity * _itemPrice;
9: }
引入解释性变量:
1: if ( (platform.toUpperCase().indexOf("MAC") > -1) &&
2: (browser.toUpperCase().indexOf("IE") > -1) &&
3: wasInitialized() && resize > 0 )
4: {
5: // do something
6: }
转化后:
1: boolean isMacOs = platform.toUpperCase().indexOf("MAC") > -1;
2: boolean isIEBrowser = browser.toUpperCase().indexOf("IE") > -1;
3: boolean wasResized = resize > 0;
4:
5: if (isMacOs && isIEBrowser && wasInitialized() && wasResized)
6: {
7: // do something
8: }
剖解临时变量:
1: double temp = 2 * (_height + _width);
2: System.out.println (temp);
3:
4: temp = _height * _width;
5: System.out.println (temp);
转化后:
1: double perimeter = 2 * (_height + _width);
2: System.out.println (perimeter);
3:
4: double area = _height * _width;
5: System.out.println (area);
移除对参数的赋值:
1: void fun(int a, int b)
2: {
3: if (a > 2)
4: a = 3
5: }
转化后:
1: void fun(int a, int b)
2: {
3: int tmp = a;
4: if ( tmp > 2)
5: tmp = 3;
6:
7: }
提前退出函数:
1: double getPayAmount() {
2: double result;
3: if (_isDead) result = deadAmount();
4: else {
5: if (_isSeparated) result = separatedAmount();
6: else {
7: if (_isRetired) result = retiredAmount();
8: else result = normalPayAmount();
9: };
10: }
11: return result;
12: };
转化后:
1: double getPayAmount() {
2: if (_isDead) return deadAmount();
3: if (_isSeparated) return separatedAmount();
4: if (_isRetired) return retiredAmount();
5: return normalPayAmount();
6: };
Split Loop:
1: void printValues() {
2: double averageAge = 0;
3: double totalSalary = 0;
4: for (int i = 0; i < people.length; i++) {
5: averageAge += people[i].age;
6: totalSalary += people[i].salary;
7: }
8:
9: averageAge = averageAge / people.length;
10:
11: System.out.println(averageAge);
12: System.out.println(totalSalary);
13: }
转化后:
1: void printValues() {
2: double totalSalary = 0;
3: for (int i = 0; i < people.length; i++) {
4: totalSalary += people[i].salary;
5: }
6:
7: double averageAge = 0;
8: for (int i = 0; i < people.length; i++) {
9: averageAge += people[i].age;
10: }
11:
12: averageAge = averageAge / people.length;
13:
14: System.out.println(averageAge);
15: System.out.println(totalSalary);
16: }
Split Loop-2
1: void printValues() {
2: System.out.println(averageAge());
3: System.out.println(totalSalary());
4: }
5:
6: private double averageAge() {
7: double result = 0;
8: for (int i = 0; i < people.length; i++) {
9: result += people[i].age;
10: }
11: return result / people.length;
12: }
13:
14: private double totalSalary() {
15: double result = 0;
16: for (int i = 0; i < people.length; i++) {
17: result += people[i].salary;
18: }
19: return result;
20: }
在对象之间搬移特性:
搬移函数(Move Method)
搬移值域(Move Field)
提炼类(Extract Class)
将类内联化(Inline Class)
隐藏[委托关系](Hide Delegate)
移除中间人(Remove Middle Man)
引入外加函数(Introduce Foreign Method)
引入本地扩展(Introduce Local Extension)
分解表达式:
1: if(date.before(SUMMER_START) || date.after(SUMMER_END))
2: charge = quantity * _winterRate + _winterServiceCharge;
3: else
4: charge = quantity * _summerRate
转化后:
1: if(notSummer(date))
2: charge = winterCharge(quantity);
3: else
4: charge = summerCharge(quantity);
Remove Double Negative
if ( !item.isNotFound() )
if ( item.isFound() )
作者:GangWang
出处:http://www.cnblogs.com/GnagWang/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。