.NET程序員不得不遵守的二十條編碼習(xí)慣
本文將為大家介紹二十條.NET程序員不得不遵守的.NET編碼習(xí)慣,希望通過(guò)這些.NET編碼習(xí)慣能讓大家的.NET開(kāi)發(fā)工作有事半功倍的效果。
1、不要硬編string/ numeric,可以使用一些常量代替。 (提高可讀性)
- int Count;
- Count = 100;
- private static const int ZERO = 0;
- if( Count == ZERO )
- {
- // 執(zhí)行一些操作
- }
2、對(duì)于字符串比較-使用String. Empty ,而不是""。
3、不要聲明成員變量為 public 或者proteted,盡量使用private 成員變量和public/protected 屬性。 (修改)
4、當(dāng)我們要在循環(huán)操作字符串,使用StringBuilder,而不是字符串,示例如下。
不好的習(xí)慣:
- String temp = String.Empty;
- for( int i = 0 ; i<= 100; i++)
- {
- temp += i.ToString();
- }
好點(diǎn)的習(xí)慣:
- StringBuilder sb = new StringBuilder();
- for ( int i = 0 ; i<= 100; i++)
- {
- 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í)慣:
- int Check = 0;
- if( Check == 0 )
- {
- // 執(zhí)行一些操作
- }
好點(diǎn)的習(xí)慣:
- bool Check = false;
- if(!Check)
- {
- // 執(zhí)行一些操作
- }
10、使用as做類(lèi)型轉(zhuǎn)換的時(shí)候,對(duì)轉(zhuǎn)換后的值進(jìn)行null值判斷
- class A
- {
- }
- class B : A
- {
- }
- B objB = new B();
- A objA1 = (A) objB;
- A objA2 = objB as A;
- if( objA2 != null)
- {
- //執(zhí)行所需的操作
- }
11、創(chuàng)建wcf代理,可以使用using表達(dá)式。 (很多地方可以這樣使用)
- using(Cerate the proxy)
- {
- //執(zhí)行所需的操作
- }
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í)例。
代碼
- class MyData
- {
- public MyData()
- {
- }
- public List<Customer> GetAllCustomer()
- {
- using (SqlConnection objConnection = new SqlConnection("Connection string"))
- {
- //執(zhí)行一些操作得到需要的數(shù)據(jù)
- }
- }
- }
如果你想創(chuàng)建的類(lèi)級(jí)別SqlConnection實(shí)例,確保您的類(lèi)實(shí)現(xiàn)了IDisposable接口,并在Dispose()中清理SqlConnection實(shí)例。
代碼
- class MyData : IDisposable
- {
- SqlConnection objConnection ;
- public MyData()
- {
- objConnection = new SqlConnection("Connection string");
- }
- public List<Customer> GetAllCustomer()
- {
- //通過(guò)objConnection得到需要的數(shù)據(jù)
- }
- public void Dispose()
- {
- //清理SqlConnection實(shí)例
- if( objConnection != null )
- {
- if( objConnection.State == ConnectionState.Open)
- {
- objConnection.Close();
- }
- }
- }
- }
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í)慣:
- for( int i = 0; i<= 100; i++)
- {
- Calculate(i);
- }
- 好點(diǎn)的習(xí)慣:
- for( int i = 0; i<= 100; i++)
- {
- //直接寫(xiě)Calculate邏輯。
- }
17、不要在循環(huán)內(nèi)處理異常,而是將循環(huán)處理的邏輯放在try/catch里面
不好的習(xí)慣:
- for(int i = 0 ; i<= 100; i++)
- {
- try
- {
- }
- catch(Exception ex)
- {
- throw ex;
- }
- }
好點(diǎn)的習(xí)慣:
- try
- {
- for(int i = 0 ; i<= 100; i++)
- {
- }
- }
- catch(Exception ex)
- {
- throw ex;
- }
18、不用通過(guò)異常處理應(yīng)用程序的邏輯
例如:
不好的習(xí)慣:
- try
- {
- int x,y,z;
- x = 0;
- y = 10;
- z = y/x;
- }
- catch(DevideByZeroException ex)
- {
- Throw ex;
- }
好點(diǎn)的習(xí)慣:
- try
- {
- int x,y,z;
- x = 0;
- y = 10;
- if( x != 0 )
- {
- z = y/x;
- }
- }
- catch(Exception ex)
- {
- }
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