下面的代码大概有几百行吧,在项目中写的一个方法,方法中的校验逻辑特别多,
如下代码结构,如何重构?使其方法中的代码能尽量简洁,明了。public bool CheckMethod(...)
{
    //下面是实例化代码
    M1BLL m1=new M1BLL();    
    M1BLL m2=new M1BLL();    
    M1BLL m3=new M1BLL();    
    M1BLL m4=new M1BLL();    
    M1BLL m5=new M1BLL(); 
    ...    //下面是局部变量
    int a,b,c;
    string d,e;
    bool fFlag=true;
    ...    foreach(classType item in ClassTypes)
    {
         if(比较方法:true)
         {
             Continue;
         }
         if(比较方法:true)
         {
             Continue;
         }
         if(比较方法:true)
         {
             Continue;
         }
         if(比较方法:true)
         {
             Continue;
         }
         if(比较方法:true)
         {
             Continue;
         }
         if(比较方法:true)
         {
             Continue;
         }
         if(比较方法:true)
         {
             Continue;
         }
         if(比较方法:true)
         {
             Continue;
         }
         if(比较方法:true)
         {
             Continue;
         }
         if(比较方法:true)
         {
             Continue;
         }
         if(比较方法:true)
         {
             Continue;
         }
         if(比较方法:true)
         {
             Continue;
         }
         if(比较方法:true)
         {
             Continue;
         }
         if(比较方法)
         {
            fFlag=false;
            break;
         }         ...         List<ClassTYpe>.Add(item);
    }    return fFlag;
}

解决方案 »

  1.   


      //下面是实例化代码
      M1BLL m1=new M1BLL();   
      M1BLL m2=new M1BLL();   
      M1BLL m3=new M1BLL();   
      M1BLL m4=new M1BLL();   
      M1BLL m5=new M1BLL();  
      ...//定义
    List<M1BLL> lstM1BLL = new List<M1BLL>();
    for(int i = 0; i < 10; i++)
    {
            M1BLL m = new M1BLL();
            lstM1BLL.Add(m);
    }//使用
    //lstM1BLL[0]使用M1BLL[]也行!嘿嘿。
      

  2.   


    //内部
    if(CheckUserName(username))
    {
    //TODO...
    }
    //方法外面
    bool CheckUserName(string username)
    {
        if(username==null)return false;
        if(BadWord(username))return false;
        if(username.Length < 3)return false;
        return true;
    }bool BadWord(string str)
    {
        //TODO...
        return true;
    }
      

  3.   

    在你的代码中,如果说要“重构”,我只想把for内部的那堆代码重构到一个单独的方法中,然后把所有的Continue改为return,仅此而已。其实也没有减少什么代码,只是让for代码看起来简单了而已。
      

  4.   

    public bool CheckMethod(...)
    {
      //下面是实例化代码
      M1BLL m1=new M1BLL();   
      M1BLL m2=new M1BLL();   
      M1BLL m3=new M1BLL();   
      M1BLL m4=new M1BLL();   
      M1BLL m5=new M1BLL(); 
    实例化有必要这么多?一个就可以了