自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

.NET程序員不得不遵守的二十條編碼習(xí)慣

開(kāi)發(fā) 后端
在這里我們將介紹二十條.NET編碼習(xí)慣,希望通過(guò)介紹這些好習(xí)慣,能讓大家的開(kāi)發(fā)工作更加輕松。

本文將為大家介紹二十條.NET程序員不得不遵守的.NET編碼習(xí)慣,希望通過(guò)這些.NET編碼習(xí)慣能讓大家的.NET開(kāi)發(fā)工作有事半功倍的效果。

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

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

2、對(duì)于字符串比較-使用String. Empty ,而不是""。

 

3、不要聲明成員變量為 public 或者proteted,盡量使用private 成員變量和public/protected 屬性。 (修改)

4、當(dāng)我們要在循環(huán)操作字符串,使用StringBuilder,而不是字符串,示例如下。

不好的習(xí)慣:

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

好點(diǎn)的習(xí)慣:

 

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

5、簡(jiǎn)單的操作,比起Collection更傾向使用Array。 (視情況,這里是建議)

 

#T#

6、比起ArrayList更傾向使用Generic Collection。 (視情況,這里是建議)

7、比起HashTable更傾向使用Generic Dictionary。 (視情況,這里是建議)

8、對(duì)于字符串的操作和存儲(chǔ),傾向于StringCollection和StringDictionary。 (視情況,這里是建議)

9、使用適合的數(shù)據(jù)類(lèi)型。

    例如:你想要判斷狀態(tài),使用bool比int要好。

不好的習(xí)慣:

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

好點(diǎn)的習(xí)慣:

 

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

10、使用as做類(lèi)型轉(zhuǎn)換的時(shí)候,對(duì)轉(zhuǎn)換后的值進(jìn)行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.   //執(zhí)行所需的操作  
  15.  } 

11、創(chuàng)建wcf代理,可以使用using表達(dá)式。 (很多地方可以這樣使用)

 

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

 12、對(duì)于昂貴的資源(例如Connection, File 等等),遵照'Acquire late, release early’ (盡量晚的獲取,盡量早的釋放)準(zhǔn)則。

 

例子:如果你想在數(shù)據(jù)操作時(shí),使用的SqlConnection對(duì)象,請(qǐng)?jiān)诜椒?jí)別,而不是在類(lèi)級(jí)別創(chuàng)建實(shí)例。

代碼 
 

  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.              //執(zhí)行一些操作得到需要的數(shù)據(jù)  
  11.          }  
  12.         
  13.       }  
  14.   } 

 如果你想創(chuàng)建的類(lèi)級(jí)別SqlConnection實(shí)例,確保您的類(lèi)實(shí)現(xiàn)了IDisposable接口,并在Dispose()中清理SqlConnection實(shí)例。

 

代碼 
 

  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.         //通過(guò)objConnection得到需要的數(shù)據(jù)  
  11.     }  
  12.     public void Dispose()  
  13.     {  
  14.         //清理SqlConnection實(shí)例  
  15.         if( objConnection != null )  
  16.         {  
  17.             if( objConnection.State == ConnectionState.Open)  
  18.             {      
  19.                objConnection.Close();  
  20.             }  
  21.         }  
  22.     }  

13、如果你不想別人擴(kuò)展你的類(lèi)功能,使用‘sealed’。

 

14、避免為每個(gè)類(lèi)都聲明‘destructor’ ,因?yàn)樗鼤?huì)增加不需要常駐內(nèi)存的類(lèi)的生命周期。

15、相對(duì)manual threading,更傾向用Thread Pool 。

16、在循環(huán)內(nèi)不要去調(diào)用其它方法。 (call function 有性能損耗)

例如:

不好的習(xí)慣:

  1. forint i = 0; i<= 100; i++)  
  2. {      
  3.    Calculate(i);  
  4. }  
  5.  好點(diǎn)的習(xí)慣:  
  6.  
  7. forint i = 0; i<= 100; i++)  
  8. {  
  9. //直接寫(xiě)Calculate邏輯。  

17、不要在循環(huán)內(nèi)處理異常,而是將循環(huán)處理的邏輯放在try/catch里面

 

不好的習(xí)慣:

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

好點(diǎn)的習(xí)慣:

 

 

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

 18、不用通過(guò)異常處理應(yīng)用程序的邏輯

例如:

不好的習(xí)慣:

  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.  } 

好點(diǎn)的習(xí)慣:

 

  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、相對(duì)for/while  ,傾向使用foreach循環(huán)。[更正]

 

20、使用多層架構(gòu)的系統(tǒng),層與層之間的交互,比起DataSet/DataTables更傾向于使用對(duì)象傳遞數(shù)據(jù)。

原文標(biāo)題:20條.net編碼習(xí)慣

鏈接:http://www.cnblogs.com/zhuqil/archive/2010/01/17/1649936.html

責(zé)任編輯:彭凡 來(lái)源: 博客園
相關(guān)推薦

2022-08-08 11:13:35

API接口前端

2019-10-23 08:54:38

程序員CPUALU

2023-11-28 09:03:50

架構(gòu)Instagram

2022-09-14 08:57:24

JavaNASA程序員

2010-11-02 14:51:11

職場(chǎng)

2019-08-09 11:40:38

JavaScriptCSS技術(shù)

2014-11-10 09:46:57

程序員

2018-08-20 13:39:15

小程序設(shè)計(jì)UI設(shè)計(jì)師

2019-10-18 17:55:03

安全運(yùn)營(yíng)

2019-12-24 14:04:59

PythonExcel數(shù)據(jù)處理

2021-04-12 08:56:00

多線(xiàn)程Future模式

2020-07-09 12:50:29

JVM內(nèi)存管理Java

2010-05-26 15:58:52

MySQL遠(yuǎn)程連接

2020-06-15 08:19:00

ZooKeeperEureka

2019-11-14 15:38:46

AndroidRelease項(xiàng)目

2011-03-31 10:46:54

LinuxCLI軟件

2009-02-23 13:00:17

程序員職業(yè)習(xí)慣

2012-12-04 10:08:25

程序員

2010-05-21 09:40:57

MySQL出錯(cuò)代碼列表

2010-05-25 09:58:43

MySQL數(shù)據(jù)庫(kù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)