.NET程序员要遵守的二十条编码习惯

本文将为大家介绍二十条.NET程序员不得不遵守的.NET编码习惯,希望通过这些.NET编码习惯能让大家的.NET开发工作有事半功倍的效果。

1、不要硬编string/ numeric,可以使用一些常量代替。 (提高可读性)

 
 
  1. int  Count;  
  2. Count = 100;  
  3. private   static   const   int  ZERO  =  0;  
  4. if (  Count  ==  ZERO )  
  5. {  
  6. // 执行一些操作  

2、对于字符串比较-使用String. Empty ,而不是""。

3、不要声明成员变量为 public 或者proteted,尽量使用private 成员变量和public/protected 属性。 (修改)

4、当我们要在循环操作字符串,使用StringBuilder,而不是字符串,示例如下。

不好的习惯:

 
 
  1. String  temp = String.Empty;  
  2.   forint  i = 0 ; i<= 100; i++)  
  3.  {  
  4.      temp += i.ToString();  
  5.  } 

好点的习惯:

 
 
  1. StringBuilder sb =  new  StringBuilder();  
  2. for  (  int  i = 0 ; i<= 100; i++)  
  3. {  
  4.     sb.Append(i.ToString());  
  5. }

    5、简单的操作,比起Collection更倾向使用Array。 (视情况,这里是建议)

    6、比起ArrayList更倾向使用Generic Collection。 (视情况,这里是建议)

    7、比起HashTable更倾向使用Generic Dictionary。 (视情况,这里是建议)

    8、对于字符串的操作和存储,倾向于StringCollection和StringDictionary。 (视情况,这里是建议)

    9、使用适合的数据类型。

    例如:你想要判断状态,使用bool比int要好。

    不好的习惯:

        
        
    1. int  Check = 0;  
    2. if ( Check == 0 )  
    3. {  
    4.      // 执行一些操作  
    5.  

    好点的习惯:

        
        
    1. bool  Check =  false ;  
    2. if (!Check)  
    3. {  
    4.      // 执行一些操作  

    10、使用as做类型转换的时候,对转换后的值进行null值判断

        
        
    1. class  A  
    2. {  
    3.  
    4. }  
    5. class  B : A  
    6. {  
    7.  
    8. }  
    9.  B objB =  new  B();  
    10.  A objA1  = (A) objB;  
    11.  A objA2 = objB  as  A;  
    12.   if ( objA2 !=  null )  
    13.  {  
    14.    //执行所需的操作  
    15.  } 

    11、创建wcf代理,可以使用using表达式。 (很多地方可以这样使用)

        
        
    1. using (Cerate the proxy)  
    2.  {  
    3.       //执行所需的操作  
    4.  } 

    12、对于昂贵的资源(例如Connection, File 等等),遵照'Acquire late, release early’
    (尽量晚的获取,尽量早的释放)准则。

    例子:如果你想在数据操作时,使用的SqlConnection对象,请在方法级别,而不是在类级别创建实例。

    代码

        
        
    1. class  MyData  
    2.   {  
    3.        public  MyData()  
    4.       {  
    5.       }  
    6.        public  List<Customer> GetAllCustomer()  
    7.       {  
    8.           using  (SqlConnection objConnection =  new  SqlConnection( "Connection string" ))  
    9.          {   
    10.               //执行一些操作得到需要的数据  
    11.          }  
    12.         
    13.       }  
    14.   } 

    如果你想创建的类级别SqlConnection实例,确保您的类实现了IDisposable接口,并在Dispose()中清理SqlConnection实例。

    代码

        
        
    1. class  MyData : IDisposable  
    2. {  
    3.     SqlConnection objConnection ;  
    4.      public  MyData()  
    5.     {   
    6.         objConnection =  new  SqlConnection( "Connection string" );  
    7.     }  
    8.      public  List<Customer> GetAllCustomer()  
    9.     {   
    10.          //通过objConnection得到需要的数据  
    11.     }  
    12.      public   void  Dispose()  
    13.     {  
    14.          //清理SqlConnection实例  
    15.          if ( objConnection !=  null  )  
    16.         {  
    17.              if ( objConnection.State == ConnectionState.Open)  
    18.             {      
    19.                objConnection.Close();  
    20.             }  
    21.         }  
    22.     }  

    13、如果你不想别人扩展你的类功能,使用‘sealed’。

    14、避免为每个类都声明‘destructor’ ,因为它会增加不需要常驻内存的类的生命周期。

    15、相对manual threading,更倾向用Thread Pool 。

    16、在循环内不要去调用其它方法。 (call function 有性能损耗)

    例如:

    不好的习惯:

        
        
    1. forint  i = 0; i<= 100; i++)  
    2. {      
    3.    Calculate(i);  
    4. }  
    5.  好点的习惯:  
    6.  
    7. forint  i = 0; i<= 100; i++)  
    8. {  
    9. //直接写Calculate逻辑。  

    17、不要在循环内处理异常,而是将循环处理的逻辑放在try/catch里面

    不好的习惯:

        
        
    1. for ( int  i = 0 ; i<= 100; i++)  
    2. {  
    3.     try  
    4.    {  
    5.    }  
    6.     catch (Exception ex)  
    7.    {  
    8.      throw  ex;  
    9.    }  

    好点的习惯:

        
        
    1. try  
    2. {  
    3.    for ( int  i = 0 ; i<= 100; i++)  
    4.   {  
    5.   }  
    6. }  
    7. catch (Exception ex)  
    8. {  
    9.      throw  ex;  

    18、不用通过异常处理应用程序的逻辑

    例如:

    不好的习惯:

        
        
    1. try  
    2. {  
    3.    int  x,y,z;  
    4.   x = 0;  
    5.   y = 10;  
    6.   z = y/x;  
    7.  }  
    8.   catch (DevideByZeroException ex)  
    9.  {  
    10.   Throw ex;  
    11.  } 

    好点的习惯:

        
        
    1. try  
    2.  {  
    3.     int  x,y,z;  
    4.    x = 0;  
    5.    y = 10;  
    6.     if ( x != 0 )  
    7.    {  
    8.       z = y/x;  
    9.    }  
    10.  }  
    11.   catch (Exception ex)  
    12.  {  
    13.  } 

    19、相对for/while  ,倾向使用foreach循环。[更正]

    20、使用多层架构的系统,层与层之间的交互,比起DataSet/DataTables更倾向于使用对象传递数据。
    原文链接:http://developer.51cto.com/art/201001/179370.htm

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值