1 private bool CheckEMailFormat()
2 {
3 lblMessage.ForeColor = System.Drawing.Color.Red;
4 System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(@"^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$");
5 if (txtAddress1.Text.Trim() != string.Empty)
6 {
7 if (!rg.IsMatch(txtAddress1.Text.Trim()))
8 {
9 lblMessage.Text = "Error:The email address you provided is invalid.Please double check it.";
10 txtAddress1.Focus();
11 return false;
12 }
13 }
14 if (txtAddress3.Text.Trim() != string.Empty)
15 {
16 System.Text.RegularExpressions.Match mt = rg.Match(txtAddress3.Text.Trim());
17 if (!mt.Success)
18 {
19 lblMessage.Text = "Error:The email address you provided is invalid.Please double check it.";
20 txtAddress3.Focus();
21 return false;
22 }
23 }
24 return true;
25 }
2:存在问题:代码冗余,函数命名不规范。2 {
3 lblMessage.ForeColor = System.Drawing.Color.Red;
4 System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(@"^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$");
5 if (txtAddress1.Text.Trim() != string.Empty)
6 {
7 if (!rg.IsMatch(txtAddress1.Text.Trim()))
8 {
9 lblMessage.Text = "Error:The email address you provided is invalid.Please double check it.";
10 txtAddress1.Focus();
11 return false;
12 }
13 }
14 if (txtAddress3.Text.Trim() != string.Empty)
15 {
16 System.Text.RegularExpressions.Match mt = rg.Match(txtAddress3.Text.Trim());
17 if (!mt.Success)
18 {
19 lblMessage.Text = "Error:The email address you provided is invalid.Please double check it.";
20 txtAddress3.Focus();
21 return false;
22 }
23 }
24 return true;
25 }
1 bool check = false;
2 check = Check();
3 if (check)
4 {
5 }else{}
6
7 // 建议修改:
8 If(CheckProcessAll())
9 {
10 }else
11 {
12 }
2 check = Check();
3 if (check)
4 {
5 }else{}
6
7 // 建议修改:
8 If(CheckProcessAll())
9 {
10 }else
11 {
12 }
3.存在问题:变量使用随意,可维护性差,Pgae中使用Session["EditUserID"]频繁
1 string strSelectUserID = Session["EditUserID"].ToString();
2 BindUserBillToNumList(strSelectUserID );
3
4 //建议修改
5 private string UserID {
6 get {
7 return Request.QueryString["UserId"] == null ?string.Empty :Request.QueryString["UserId"].ToString();
8 }
9 }
10 BindUserBillToNumList(this.UserID);
4.存在问题:逻辑划分不清,代码重复量大,可维护性差2 BindUserBillToNumList(strSelectUserID );
3
4 //建议修改
5 private string UserID {
6 get {
7 return Request.QueryString["UserId"] == null ?string.Empty :Request.QueryString["UserId"].ToString();
8 }
9 }
10 BindUserBillToNumList(this.UserID);
1 protected void btnUpdate_Click(object sender, EventArgs e)
2 {
3 Guid pBillInfoID = Guid.Empty;
4 //此处省略200行
5 LogService.Instance.WriteALog(log);
6 }
7
8 //建议修改:
9 protected void btnUpdate_Click(object sender, EventArgs e)
10 {
11 Guid pBillInfoID = Guid.Empty;
12 调用处理的函数
13 LogService.Instance.WriteALog(log);
14 }
15
5.存在问题:随意使用数据库链接,对开源模块了解不清,效率低下,可维护性差2 {
3 Guid pBillInfoID = Guid.Empty;
4 //此处省略200行
5 LogService.Instance.WriteALog(log);
6 }
7
8 //建议修改:
9 protected void btnUpdate_Click(object sender, EventArgs e)
10 {
11 Guid pBillInfoID = Guid.Empty;
12 调用处理的函数
13 LogService.Instance.WriteALog(log);
14 }
15
1 protected DataTable InitWebOrderReportTb(string str)
2 {
3 DataTable webOrderReport = new DataTable();
4 IList<OrderEntity> ilstOrderEntity = OrderService.Instance.GetAllOrderEntityByUserID(str);
5 for (int i = 0; i <= ilstOrderEntity.Count - 1; i++)
6 {
7 SubBrandEntity subbrandEntity = SubBrandService.Instance.GetSingleById(ilstOrderEntity[i].SubBrand_ID);
8 BrandEntity brandEntity = BrandService.Instance.GetBrandById(subbrandEntity.Brand_ID);
9 UserShipInfoEntity shipInfoEntity = UserService.Instance.GetSingleShipInfo(ilstOrderEntity[i].UserShipInfo_ID);
10 IList<OrderItemEntity> ilstOrderItemEntity = OrderService.Instance.GetAllOrderItemByOrderID(ilstOrderEntity[i].Order_ID);
11 for (int j = 0; j <= ilstOrderItemEntity.Count - 1; j++)
12 {
13 ProductEntity productEntity = ProductService.Instance.GetSingleProduct(ilstOrderItemEntity[j].Product_ID);
14 ContractItemEntity contractItemEntity = ContractService.Instance.GetSingleContractItem(ilstOrderItemEntity[j].ContractItem_ID);
15 if (contractItemEntity != null)
16 {
17 ContractEntity contractEntity = ContractService.Instance.GetSingleContract(contractItemEntity.Contract_ID);
18 if (contractEntity != null)
19 {
20 IList<ContractItemAttributeEntity> ilstAttributeEntity = ContractService.Instance.GetContractItemAttrbByCond(contractEntity.Contract_ID);
21 for (int k = 0; k <= ilstAttributeEntity.Count - 1; k++)
22 {
23 //.
24 }
25 }
26
27 }
28 }
29 }
30 return webOrderReport;
31 }
32 //建议修改:使用数据库存储过程来处理
33
这些代码是从一个具体的项目中摘抄出来的,这个项目比较有意思,最初参与的人员超过10个人,项目在地理位置上也比较特殊,有西安本部,西安本部实习生,西安外包人员,上海本部人员,上海外驻人员,宁波的外包开发人员.项目到后期的维护工作量极大,后期代码被大量重写,而且维护时间超过了逾期的开发时间.这些代码没有什么实用的价值,但是从开发这么多年的经验来看,这种代码看见过很多.2 {
3 DataTable webOrderReport = new DataTable();
4 IList<OrderEntity> ilstOrderEntity = OrderService.Instance.GetAllOrderEntityByUserID(str);
5 for (int i = 0; i <= ilstOrderEntity.Count - 1; i++)
6 {
7 SubBrandEntity subbrandEntity = SubBrandService.Instance.GetSingleById(ilstOrderEntity[i].SubBrand_ID);
8 BrandEntity brandEntity = BrandService.Instance.GetBrandById(subbrandEntity.Brand_ID);
9 UserShipInfoEntity shipInfoEntity = UserService.Instance.GetSingleShipInfo(ilstOrderEntity[i].UserShipInfo_ID);
10 IList<OrderItemEntity> ilstOrderItemEntity = OrderService.Instance.GetAllOrderItemByOrderID(ilstOrderEntity[i].Order_ID);
11 for (int j = 0; j <= ilstOrderItemEntity.Count - 1; j++)
12 {
13 ProductEntity productEntity = ProductService.Instance.GetSingleProduct(ilstOrderItemEntity[j].Product_ID);
14 ContractItemEntity contractItemEntity = ContractService.Instance.GetSingleContractItem(ilstOrderItemEntity[j].ContractItem_ID);
15 if (contractItemEntity != null)
16 {
17 ContractEntity contractEntity = ContractService.Instance.GetSingleContract(contractItemEntity.Contract_ID);
18 if (contractEntity != null)
19 {
20 IList<ContractItemAttributeEntity> ilstAttributeEntity = ContractService.Instance.GetContractItemAttrbByCond(contractEntity.Contract_ID);
21 for (int k = 0; k <= ilstAttributeEntity.Count - 1; k++)
22 {
23 //.
24 }
25 }
26
27 }
28 }
29 }
30 return webOrderReport;
31 }
32 //建议修改:使用数据库存储过程来处理
33
些感谢昨天给出代码那些朋友,这些代码仅仅作为一个方面的例子,抛块转头出来,同样还是也昨天的议题一样,在在这里再次征集方面代码,在这里我们看看让我们头疼的东西是什么样子的.
本文转自小余(Yice)博客园博客,原文链接: http://www.cnblogs.com/yice/archive/2008/04/17/943458.html ,如需转载请自行联系原作者