下面的代码大概有几百行吧,在项目中写的一个方法,方法中的校验逻辑特别多,
如下代码结构,如何重构?使其方法中的代码能尽量简洁,明了。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;
}
如下代码结构,如何重构?使其方法中的代码能尽量简洁,明了。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;
}
//下面是实例化代码
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[]也行!嘿嘿。
//内部
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;
}
{
//下面是实例化代码
M1BLL m1=new M1BLL();
M1BLL m2=new M1BLL();
M1BLL m3=new M1BLL();
M1BLL m4=new M1BLL();
M1BLL m5=new M1BLL();
实例化有必要这么多?一个就可以了