修改了别人的代码、优化前与优化后的对比,唉,写个好代码真不容易啊

   最近紧急接手别人做了一半的项目,看了一些代码,惨不忍睹,也号称是上海的IT公司写的代码,还说自己是做了很多年的政府项目,自信得很,态度很高傲、目中无人,我是彻底服了,我已经很嚣张了,居然比我还嚣张很多很多,项目做得一塌糊涂,代码质量还这么差,当然也不是说我的代码写得有多好,只是想说,写代码需要认真再认真,需要仔细做好每件事、日积月累,才能有个良好的技术积累、项目积累,想赚钱也会容易很多。

   对方页面、代码、数据、里主要问题有:

      1:命名不规范。

      2:排版也乱。

      3:一个注释也没有。

      4:代码复用率低,同样功能的代码有重复出现的。

      5:思路有些混乱,代码不严格。

      6:程序运行有严重的Bug,默认加载居然后面的市不会出来。

      7:数据丢失,省的选项里,居然没有湖北、还有些省份的数据也乱了套。

扫描二维码关注公众号,回复: 6504320 查看本文章

      8:默认加载指定的省、市功能不稳定。

      9:其他更多的细节,就不指望了,能正确跑起来就不错了。

   当然我没按完美程度要求,只是略微调整了一些,这么简单的东西,都能写成这样,更有点儿难度的页面,能写成什么呢?我都不好意思贴出来其他模块的代码,修改完善别人的代码,真是痛苦啊,只是没办法全部都重新写了,只能错在哪里,修改哪里,维护哪里。

   我们是想强调一点儿,若简单的东西都做不好的,整个项目一定会更乱,质量是不敢想象的,我们只有做好每一点每一滴,才有希望把整个项目做得很棒。

   你可以说,这个不重要、那个不重要、那什么才重要?只有出了人命才重要吗?

  优化前代码:

28171252_7rlI.gif 代码
using  System;
using  System.Collections;
using  System.Collections.Generic;
using  System.Configuration;
using  System.Data;
using  System.Linq;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.HtmlControls;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Xml.Linq;
using  ClientObjects;
using  ModelObject;
namespace  Festind.Control
{
    
public   partial   class  cityControl : System.Web.UI.UserControl
    {
        
public   int  _PID
        {
            
set  { pid.Value  =  value.ToString(); }
            
get
            {
                
try
                {
                    
return   int .Parse(ddp.SelectedValue);
                }
                
catch  {  return   0 ; }
            }
        }
        
public   int  _CID
        {
            
set  { cid.Value  =  value.ToString(); }
            
get
            {
                
try
                {
                    
return   int .Parse(ddc.SelectedValue);
                }
                
catch  {  return   0 ; }
            }
        }

        
protected   void  Page_Load( object  sender, EventArgs e)
        {
            
if  ( ! IsPostBack)
            {
                List
< DictionaryModel >  lst  =  CDictionary.getDictionary(DictionaryModel.DictionaryType.PROVINCE);
                ddp.Items.Clear();
                
for  ( int  i  =   0 ; i  <  lst.Count; i ++ )
                {
                    ddp.Items.Add(
new  ListItem(lst[i].Value, lst[i].ID.ToString()));
                }
                ddp.SelectedValue 
=  pid.Value;
                lst 
=  CDictionary.getDictionary(DictionaryModel.DictionaryType.CITY,  int .Parse(pid.Value));
                ddc.Items.Clear();
                
for  ( int  i  =   0 ; i  <  lst.Count; i ++ )
                {
                    ddc.Items.Add(
new  ListItem(lst[i].Value, lst[i].ID.ToString()));
                }
                ddc.SelectedValue 
=  cid.Value;
            }
        }

        
protected   void  ddp_SelectedIndexChanged( object  sender, EventArgs e)
        {
            
int  id  =   int .Parse(ddp.SelectedValue);
            List
< DictionaryModel >  lst  =  CDictionary.getDictionary(DictionaryModel.DictionaryType.CITY, id);
            ddc.Items.Clear();
            
for  ( int  i  =   0 ; i  <  lst.Count; i ++ )
            {
                ddc.Items.Add(
new  ListItem(lst[i].Value, lst[i].ID.ToString()));
            }
        }
        
public   void  load()
        {
            ddp.SelectedValue 
=  pid.Value;
            List
< DictionaryModel >  lst  =  CDictionary.getDictionary(DictionaryModel.DictionaryType.CITY,  int .Parse(pid.Value));
            ddc.Items.Clear();
            
for  ( int  i  =   0 ; i  <  lst.Count; i ++ )
            {
                ddc.Items.Add(
new  ListItem(lst[i].Value, lst[i].ID.ToString()));
            }
            ddc.SelectedValue 
=  cid.Value;
        }
    }
}

  

  优化后代码:

28171252_7rlI.gif 代码
using  System;
using  System.Collections;
using  System.Collections.Generic;
using  System.Configuration;
using  System.Data;
using  System.Linq;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.HtmlControls;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Xml.Linq;
using  ClientObjects;
using  ModelObject;

namespace  Festind.Control
{
    
public   partial   class  CityControl : System.Web.UI.UserControl
    {
        
///   <summary>
        
///  省
        
///   </summary>
         public   int  ProvinceID
        {
            
set
            {
                
this .provinceID.Value  =  value.ToString();
            }
            
get
            {
                
try
                {
                    
return   int .Parse( this .ddlProvince.SelectedValue);
                }
                
catch
                {
                    
return   0
                }
            }
        }

        
///   <summary>
        
///  市
        
///   </summary>
         public   int  CityID
        {
            
set
            {
                
this .cityID.Value  =  value.ToString();
            }
            
get
            {
                
try
                {
                    
return   int .Parse( this .ddlCity.SelectedValue);
                }
                
catch
                {
                    
return   0 ;
                }
            }
        }

        
protected   void  Page_Load( object  sender, EventArgs e)
        {
            
if  ( ! this .IsPostBack)
            {
                
this .Load( this .provinceID.Value,  this .cityID.Value);
            }
        }

        
protected   void  ddlProvince_SelectedIndexChanged( object  sender, EventArgs e)
        {
            
this .Load( this .ddlProvince.SelectedValue);
        }

        
public   void  Load()
        {
            
this .Load( this .provinceID.Value,  this .cityID.Value);
        }

        
private   void  Load(String province)
        {
            
this .ddlCity.Items.Clear();
            List
< DictionaryModel >  lstCity  =  CDictionary.getDictionary(DictionaryModel.DictionaryType.CITY,  int .Parse(province));
            
for  ( int  i  =   0 ; i  <  lstCity.Count; i ++ )
            {
                
this .ddlCity.Items.Add( new  ListItem(lstCity[i].Value, lstCity[i].ID.ToString()));
            }
        }

        
public   void  Load(String province, String city)
        {
            
//  加载省
             this .ddlProvince.Items.Clear();
            List
< DictionaryModel >  lstProvince  =  CDictionary.getDictionary(DictionaryModel.DictionaryType.PROVINCE);
            
for  ( int  i  =   0 ; i  <  lstProvince.Count; i ++ )
            {
                
this .ddlProvince.Items.Add( new  ListItem(lstProvince[i].Value, lstProvince[i].ID.ToString()));
            }
            
if  ( ! province.Equals( " 0 " ))
            {
                
this .ddlProvince.SelectedValue  =  province;
            }
            
else
            {
                province 
=   this .ddlProvince.SelectedValue;
            }
            
            
//  加载市
             this .Load(province);
            
if  ( ! city.Equals( " 0 " ))
            {
                
this .ddlCity.SelectedValue  =  city;
            }
        }
    }
}

转载于:https://my.oschina.net/iwenr/blog/227851

猜你喜欢

转载自blog.csdn.net/weixin_34344403/article/details/91674586