从软件的易维护角度帮忙简化下面这个函数(从根本上简化,尽量不要再拆分成小函数)
------
//显示设备基本信息列表
void __fastcall TForm1::ShowDevInfoGrid()
{
for(int i=0; i <DevInfoGrid-> RowCount; i++) //清空显示框
for(int j=0; j <DevInfoGrid-> ColCount; j++)
DevInfoGrid-> Cells[j][i] = " ";
DevInfoGrid-> RowCount = 2;
//显示初始的常量
DevInfoGrid-> Cells[1][0] = "设备编号 ";
DevInfoGrid-> Cells[2][0] = "设备号 ";
DevInfoGrid-> Cells[3][0] = "设备名称 ";
DevInfoGrid-> Cells[4][0] = "设备型号及规格 ";
DevInfoGrid-> Cells[5][0] = "设备类型 ";
DevInfoGrid-> Cells[6][0] = "设备厂牌 ";
DevInfoGrid-> Cells[0][1] = "1 ";
if(SelectLineNumOffice == " ")
{
if(SelectLineNumShop == " ")
{
if(SelectLineNumStation == " ")
{
if(SelectLineNum != " ") //目录树中选择了生产线
{
//根据生产线名称检索出生产线编号
Query1-> Close();
Query1-> SQL-> Clear();
Query1-> SQL-> Add( "select DevGroupNO from DevGroup_t where DevGroupName=:DevGrpName ");
Query1-> ParamByName( "DevGrpName ")-> AsString = SelectLineNum;
if(Query1-> Prepared == false)
Query1-> Prepare();
Query1-> Open();
int DevGrpNO = Query1-> FieldByName( "DevGroupNO ")-> AsInteger;
//根据设备组编号检索出设备编号
Query1-> Close();
Query1-> SQL-> Clear();
Query1-> SQL-> Add( "select DeviceNO from DevGroupDev_t where DevGroupNO=:DevGrpNO order by DeviceNO ");
Query1-> ParamByName( "DevGrpNO ")-> AsInteger = DevGrpNO;
if(Query1-> Prepared == false)
Query1-> Prepare();
Query1-> Open();
//记录设备编号
TStringList *tsl = new TStringList();
for(int i=0; i <Query1-> RecordCount; i++)
{
tsl-> Add(IntToStr(Query1-> FieldByName( "DeviceNO ")-> AsInteger));
Query1-> Next();
}
//根据设备编号检索出设备的相关信息
for(int i=0; i <tsl-> Count; i++)
{
Query1-> Close();
Query1-> SQL-> Clear();
Query1-> SQL-> Add( "select * from DevInfo_t where DeviceNO=:DevNO ");
Query1-> ParamByName( "DevNO ")-> AsInteger = StrToInt(tsl-> Strings[i]);
if(Query1-> Prepared == false)
Query1-> Prepare();
Query1-> Open();
DevInfoGrid-> Cells[0][DevInfoGrid-> RowCount - 1] = IntToStr(DevInfoGrid-> RowCount - 1);
DevInfoGrid-> Cells[1][DevInfoGrid-> RowCount - 1] = IntToStr(Query1-> FieldByName( "DeviceNO ")-> AsInteger);
DevInfoGrid-> Cells[2][DevInfoGrid-> RowCount - 1] = Query1-> FieldByName( "DeviceID ")-> AsString;
DevInfoGrid-> Cells[3][DevInfoGrid-> RowCount - 1] = Query1-> FieldByName( "DeviceName ")-> AsString;
DevInfoGrid-> Cells[4][DevInfoGrid-> RowCount - 1] = Query1-> FieldByName( "DevSpecType ")-> AsString;
DevInfoGrid-> Cells[5][DevInfoGrid-> RowCount - 1] = Query1-> FieldByName( "DevType ")-> AsString;
DevInfoGrid-> Cells[6][DevInfoGrid-> RowCount - 1] = Query1-> FieldByName( "Brand ")-> AsString;
if(i < tsl-> Count - 1)
DevInfoGrid-> RowCount = DevInfoGrid-> RowCount +1; //不是最后行则增加一行用于显示记录
}
delete tsl;
}
}
else //目录树中选择了工作站
{
//根据工作站编号查出设备组编号
Query1-> Close();
Query1-> SQL-> Clear();
Query1-> SQL-> Add( "select DevGroupNO from DevDistribute_t where WorkStationID=:WSID order by DevGroupNO ");
Query1-> ParamByName( "WSID ")-> AsString = SelectLineNumStation;
if(Query1-> Prepared == false)
Query1-> Prepare();
Query1-> Open();
//记住设备组编号
TStringList *tsl = new TStringList();
for(int i=0; i <Query1-> RecordCount; i++)
{
tsl-> Add(IntToStr(Query1-> FieldByName( "DevGroupNO ")-> AsInteger));
Query1-> Next();
}
//根据设备组编号查出设备的编号
TStringList *DevTsl = new TStringList();
for(int i=0; i <tsl-> Count; i++)
{
Query1-> Close();
Query1-> SQL-> Clear();
Query1-> SQL-> Add( "Select DeviceNO from DevGroupDev_t where DevGroupNO=:DevGrpNO order by DeviceNO ");
Query1-> ParamByName( "DevGrpNO ")-> AsInteger = StrToInt(tsl-> Strings[i]);
if(Query1-> Prepared == false)
Query1-> Prepare();
Query1-> Open();
for(int i=0; i <Query1-> RecordCount; i++)
{
if(Query1-> FieldByName( "DeviceNO ")-> AsInteger)
DevTsl-> Add(IntToStr(Query1-> FieldByName( "DeviceNO ")-> AsInteger));
Query1-> Next();
}
}
//根据设备编号检索出相关信息
for(int i=0; i <DevTsl-> Count; i++)
{
Query1-> Close();
Query1-> SQL-> Clear();
Query1-> SQL-> Add( "select * from DevInfo_t where DeviceNO=:DevNO ");
Query1-> ParamByName( "DevNO ")-> AsInteger = StrToInt(DevTsl-> Strings[i]);
if(Query1-> Prepared == false)
Query1-> Prepare();
Query1-> Open();
DevInfoGrid-> Cells[0][DevInfoGrid-> RowCount - 1] = IntToStr(DevInfoGrid-> RowCount - 1);
DevInfoGrid-> Cells[1][DevInfoGrid-> RowCount - 1] = IntToStr(Query1-> FieldByName( "DeviceNO ")-> AsInteger);
DevInfoGrid-> Cells[2][DevInfoGrid-> RowCount - 1] = Query1-> FieldByName( "DeviceID ")-> AsString;
DevInfoGrid-> Cells[3][DevInfoGrid-> RowCount - 1] = Query1-> FieldByName( "DeviceName ")-> AsString;
DevInfoGrid-> Cells[4][DevInfoGrid-> RowCount - 1] = Query1-> FieldByName( "DevSpecType ")-> AsString;
DevInfoGrid-> Cells[5][DevInfoGrid-> RowCount - 1] = Query1-> FieldByName( "DevType ")-> AsString;
DevInfoGrid-> Cells[6][DevInfoGrid-> RowCount - 1] = Query1-> FieldByName( "Brand ")-> AsString;
if(i < DevTsl-> Count - 1)
DevInfoGrid-> RowCount = DevInfoGrid-> RowCount +1; //不是最后行则增加一行用于显示记录
}
delete DevTsl;
delete tsl;
}
}
else //目录树中选择了车间
{
//根据车间编号查出设备组编号
Query1-> Close();
Query1-> SQL-> Clear();
Query1-> SQL-> Add( "select DevGroupNO from DevDistribute_t where WorkShopID=:WSID order by DevGroupNO ");
Query1-> ParamByName( "WSID ")-> AsString = SelectLineNumShop;
if(Query1-> Prepared == false)
Query1-> Prepare();
Query1-> Open();
//记住设备组编号
TStringList *tsl = new TStringList();
for(int i=0; i <Query1-> RecordCount; i++)
{
tsl-> Add(IntToStr(Query1-> FieldByName( "DevGroupNO ")-> AsInteger));
Query1-> Next();
}
//根据设备组编号查出设备的编号
TStringList *DevTsl = new TStringList();
for(int i=0; i <tsl-> Count; i++)
{
Query1-> Close();
Query1-> SQL-> Clear();
Query1-> SQL-> Add( "Select DeviceNO from DevGroupDev_t where DevGroupNO=:DevGrpNO order by DeviceNO ");
Query1-> ParamByName( "DevGrpNO ")-> AsInteger = StrToInt(tsl-> Strings[i]);
if(Query1-> Prepared == false)
Query1-> Prepare();
Query1-> Open();
for(int i=0; i <Query1-> RecordCount; i++)
{
if(Query1-> FieldByName( "DeviceNO ")-> AsInteger)
DevTsl-> Add(IntToStr(Query1-> FieldByName( "DeviceNO ")-> AsInteger));
Query1-> Next();
}
}
//根据设备编号检索出相关信息
for(int i=0; i <DevTsl-> Count; i++)
{
Query1-> Close();
Query1-> SQL-> Clear();
Query1-> SQL-> Add( "select * from DevInfo_t where DeviceNO=:DevNO ");
Query1-> ParamByName( "DevNO ")-> AsInteger = StrToInt(DevTsl-> Strings[i]);
if(Query1-> Prepared == false)
Query1-> Prepare();
Query1-> Open();
DevInfoGrid-> Cells[0][DevInfoGrid-> RowCount - 1] = IntToStr(DevInfoGrid-> RowCount - 1);
DevInfoGrid-> Cells[1][DevInfoGrid-> RowCount - 1] = IntToStr(Query1-> FieldByName( "DeviceNO ")-> AsInteger);
DevInfoGrid-> Cells[2][DevInfoGrid-> RowCount - 1] = Query1-> FieldByName( "DeviceID ")-> AsString;
DevInfoGrid-> Cells[3][DevInfoGrid-> RowCount - 1] = Query1-> FieldByName( "DeviceName ")-> AsString;
DevInfoGrid-> Cells[4][DevInfoGrid-> RowCount - 1] = Query1-> FieldByName( "DevSpecType ")-> AsString;
DevInfoGrid-> Cells[5][DevInfoGrid-> RowCount - 1] = Query1-> FieldByName( "DevType ")-> AsString;
DevInfoGrid-> Cells[6][DevInfoGrid-> RowCount - 1] = Query1-> FieldByName( "Brand ")-> AsString;
if(i < DevTsl-> Count - 1)
DevInfoGrid-> RowCount = DevInfoGrid-> RowCount +1; //不是最后行则增加一行用于显示记录
}
delete DevTsl;
delete tsl;
}
}
else //目录树选择了办公室
{
Query1-> Close();
Query1-> SQL-> Clear();
Query1-> SQL-> Add( "select * from DevInfo_t order by DeviceNO ");
if(Query1-> Prepared == false)
Query1-> Prepare();
Query1-> Open();
DevInfoGrid-> RowCount = Query1-> RecordCount + 1;
for(int i=0; i <DevInfoGrid-> RowCount; i++)
{
DevInfoGrid-> Cells[0][i+1] = IntToStr(i + 1);
DevInfoGrid-> Cells[1][i+1] = IntToStr(Query1-> FieldByName( "DeviceNO ")-> AsInteger);
DevInfoGrid-> Cells[2][i+1] = Query1-> FieldByName( "DeviceID ")-> AsString;
DevInfoGrid-> Cells[3][i+1] = Query1-> FieldByName( "DeviceName ")-> AsString;
DevInfoGrid-> Cells[4][i+1] = Query1-> FieldByName( "DevSpecType ")-> AsString;
DevInfoGrid-> Cells[5][i+1] = Query1-> FieldByName( "DevType ")-> AsString;
DevInfoGrid-> Cells[6][i+1] = Query1-> FieldByName( "Brand ")-> AsString;
Query1-> Next();
}
}
}
//---------------------------------------------------------------------------
------
这么长的一个函数,不分成几个小部分是不可想象的。
------
gz tmf
------
BCB技术内幕推荐,超过20行的代码就应该独立为一个函数!我觉得非常有道理!
------
最起码要将给DevInfoGrid填充的部分拿出来做成function:
void __fastcall TForm1::FillGridFromQuery(TQuery *DestQuery)
还可以将查询做成function:
MyQuery(.....)
------
cc
------
不是这个意思,我说的是可以修改SQL语句来简化函数的代码行,可是我对SQL不熟悉,不会简化
------
完全同意wangledong(快乐的程序员)
我的经验就是这样的,如果是一个算法程序,也应控制在100行内,再超过的话就应该单独写一个类。
------
太长了, 不如你把意图说一下,这样好讨论。
------
其实,就是操作数据库部分占用代码太多了
经人提醒,使用存储过程能减少不少的代码行
------
用存储过程是最好的解决办法!!!
但是在这里也可以做一些简化,如:
//根据生产线名称检索出生产线编号
Query1-> Close();
Query1-> SQL-> Clear();
Query1-> SQL-> Add( "select DevGroupNO from DevGroup_t where DevGroupName=:DevGrpName ");
Query1-> ParamByName( "DevGrpName ")-> AsString = SelectLineNum;
if(Query1-> Prepared == false)
Query1-> Prepare();
Query1-> Open();
int DevGrpNO = Query1-> FieldByName( "DevGroupNO ")-> AsInteger;
//根据设备组编号检索出设备编号
Query1-> Close();
Query1-> SQL-> Clear();
Query1-> SQL-> Add( "select DeviceNO from DevGroupDev_t where DevGroupNO=:DevGrpNO order by DeviceNO ");
Query1-> ParamByName( "DevGrpNO ")-> AsInteger = DevGrpNO;
if(Query1-> Prepared == false)
Query1-> Prepare();
Query1-> Open();
//记录设备编号
TStringList *tsl = new TStringList();
for(int i=0; i <Query1-> RecordCount; i++)
{
tsl-> Add(IntToStr(Query1-> FieldByName( "DeviceNO ")-> AsInteger));
Query1-> Next();
可以改成如下:
//根据生产线名称检索出生产线编号
Query1-> Close();
Query1-> SQL-> Clear();
Query1-> SQL-> Add( "select DeviceNO from DevGroupDev_t where
DevGroupNO= ");
Query1-> SQL-> Add( "(select DevGroupNO from DevGroup_t where DevGroupName=:DevGrpName) order by DeviceNO ");
Query1-> ParamByName( "DevGrpName ")-> AsString = SelectLineNum;
Query1-> Open();
//记录设备编号
TStringList *tsl = new TStringList();
for(int i=0; i <Query1-> RecordCount; i++)
{
tsl-> Add(IntToStr(Query1-> FieldByName( "DeviceNO ")-> AsInteger));
Query1-> Next();
}
------
谢谢楼上这位,看来我对SQL语句还得好好了解啊;)
------
//把字符串标示改为int型将if else 改switch,尽量的把共同的代码合并。
Query1-> Close();
Query1-> SQL-> Clear();
switch(SelNodeType)
{
case 1: //生产线
SqlStr = " "
Query1-> ParamByName( "DevGrpName ")-> AsString = SelectLineNum;
break;
case 2: //
......
}
.....
桂ICP备07017180号