首页 > 学院 > 开发设计 > 正文

敏捷开发技巧-消除代码异味

2019-11-18 15:29:34
字体:
来源:转载
供稿:网友

摘要:
本文通过简单通俗的例子, 告诉我们如何判定代码的稳定性和代码中的异类, 并且如何重构此类代码.

异味这个词,可能有点抽象,我们先看一下下面的例子

这是一个CAD系统. 现在,它已经可以画三种外形了:线条,长方形,跟圆.先认真的看一下下面的代码:
class Shape {                                                                                   
       final static int TYPELINE = 0;                                                               
       final static int TYPERECTANGLE = 1;                                                          
       final static int TYPECIRCLE = 2;                                                             
       int shapeType;                                                                               
       //线条的开始点
     //长方形左下角的点
     //圆心
     Point p1;                                                                                    
       //线条的结束点
     //长方形的右上角的点
     //假如是圆的话,这个属性不用
     Point p2;                                                                                    
       int radius;                                                                                  
    }                                                                                               
class CADApp {                                                                                  
       void drawShapes(Graphics graphics, Shape shapes[]) {                                         
           for (int i = 0; i < shapes.length; i++) {                                                
               switch (shapes[i].getType()) {                                                       
                  case Shape.TYPELINE:                                                              
                      graphics.drawLine(shapes[i].getP1(), shapes[i].getP2());                      
                      break;                                                                        
                  case Shape.TYPERECTANGLE:                                                         
                      //画四条边
               graphics.drawLine(...);                                                       
                      graphics.drawLine(...);                                                       
                      graphics.drawLine(...);                                                       
                      graphics.drawLine(...);                                                       
                      break;                                                                        
                  case Shape.TYPECIRCLE:                                                            
                      graphics.drawCircle(shapes[i].getP1(), shapes[i].getRadius());                
                      break;                                                                        
               }                                                                                    
           }                                                                                        
       }                                                                                            
    } 
                                                                                              

    代码都是一直在改变的,而这也是上面的代码会碰到的一个问题.

    现在我们有一个问题: 假如我们需要支持更多的外形(比如三角形), 那么肯定要改动Shape这个类, CADApp里面的drawShapes这个方法也要改.
好,改为如下的样子:
    
class Shape {      
       final static int TYPELINE = 0;
       final static int TYPERECTANGLE = 1;
       final static int TYPECIRCLE = 2;
       final static int TYPETRIANGLE = 3;
       int shapeType;  
       Point p1;       
       Point p2;       
       //三角形的第三个点.
       Point p3;       
       int radius;     
    }                  
class CADApp {     
     void drawShapes(Graphics graphics, Shape shapes[]) {
         for (int i = 0; i < shapes.length; i++) {
             switch (shapes[i].getType()) {
                case Shape.TYPELINE:
                    graphics.drawLine(shapes[i].getP1(), shapes[i].getP2());
                    break;
                case Shape.TYPERECTANGLE:
                      //画四条边.
                    graphics.drawLine(...);
                    graphics.drawLine(...);
                    graphics.drawLine(...);
                    graphics.drawLine(...);
                    break;
                case Shape.TYPECIRCLE:
                    graphics.drawCircle(shapes[i].getP1(), shapes[i].getRadius());
                    break;
                case Shape.TYPETRIANGLE:
                    graphics.drawLine(shapes[i].getP1(), shapes[i].getP2());
                    graphics.drawLine(shapes[i].getP2(), shapes[i].getP3());
                    graphics.drawLine(shapes[i].getP3(), shapes[i].getP1());
                    break;
               }       
           }           
       }               
  }
                  

    假如以后要支持更多的外形,这些类又要改动……,这可不是什么好事情!
   理想情况下,我们希望当一个类,一个方法或其他的代码设计完以后,就不用再做修改了。它们应该稳定到不用修改就可以重用。 
    现在的情况恰好相反!
    每当我们增加新的外形,都得修改Shape这个类,跟CADApp里面的drawShapes方法。

    怎么让代码稳定(也就是无需修改)?这个问题是个好问题!不过老规矩,先不说,我们以行动回答。
    我们先看看另外一个方法: 当给你一段代码,你怎么知道它是稳定的?

 



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表