C# 編碼規(guī)范和編程好習(xí)慣
誰都會(huì)寫代碼!幾個(gè)月的編程經(jīng)驗(yàn)可以讓你寫出“可運(yùn)行應(yīng)用程序”。讓它可運(yùn)行容易,但是以最有效率的方式編碼就需要下更多的功夫!
要知道,大多數(shù)程序員在寫”可運(yùn)行代碼,“而不是”高效代碼“。我們?cè)谶@個(gè)指南課程前面提到,你想成為你們公司”最尊貴的專業(yè)人員“嗎?寫”高效代碼“是一項(xiàng)藝術(shù),你必須學(xué)習(xí)和實(shí)踐它。
命名慣例和規(guī)范
注記 :
Pascal 大小寫形式-所有單詞第一個(gè)字母大寫,其他字母小寫。
Camel 大小寫形式-除了第一個(gè)單詞,所有單詞第一個(gè)字母大寫,其他字母小寫。
public class HelloWorld{ ...}public class HelloWorld{ void SayHello(string name) { ... }}
public class HelloWorld{ int totalCount = 0; void SayHello(string name) { string fullMessage = "Hello " + name; ... }}
以前,多數(shù)程序員喜歡它-把數(shù)據(jù)類型作為變量名的前綴而m_作為成員變量的前綴。例如:
string m_sName;int nAge;然而,這種方式在.NET編碼規(guī)范中是不推薦的。所有變量都用camel 大小寫形式,而不是用數(shù)據(jù)類型和m_來作前綴。
- 別用縮寫。用name, address, salary等代替 nam, addr, sal
- 別使用單個(gè)字母的變量象i, n, x 等. 使用 index, temp等
用于循環(huán)迭代的變量例外:
for ( int i = 0; i < count; i++ ){ ...}如果變量只用于迭代計(jì)數(shù),沒有在循環(huán)的其他地方出現(xiàn),許多人還是喜歡用單個(gè)字母的變量(i) ,而不是另外取名。 - 變量名中不使用下劃線 (_) 。
- 命名空間需按照標(biāo)準(zhǔn)的模式命名
. . .
例如,對(duì)于類HelloWorld, 相應(yīng)的文件名應(yīng)為 helloworld.cs (或, helloworld.vb)
縮進(jìn)和間隔
bool SayHello (string name) { string fullMessage = "Hello " + name; DateTime currentTime = DateTime.Now; string message = fullMessage + ", the time is : " + currentTime.ToShortTimeString(); MessageBox.Show ( message ); if ( ... ) { // Do something // ... return false; } return true; } 這段代碼看起來比上面的好:: bool SayHello ( string name ) { string fullMessage = "Hello " + name; DateTime currentTime = DateTime.Now;
string message = fullMessage + ", the time is : " + currentTime.ToShortTimeString();
MessageBox.Show ( message );
if ( ... ) { // Do something // ...
return false; }
return true; }
好:
if ( ... ) { // Do something }不好: if ( ... ) { // Do something }
好:
if ( showResult == true ) { for ( int i = 0; i < 10; i++ ) { // } }不好: if(showResult==true) { for(int i= 0;i<10;i++) { // } }
良好的編程習(xí)慣
遵從以下良好的習(xí)慣以寫出好程序
好:
void SavePhoneNumber ( string phoneNumber ) { // Save the phone number. }不好:
// This method will save the phone number. void SaveData ( string phoneNumber ) { // Save the phone number. }
好:
// Save the address. SaveAddress ( address ); // Send an email to the supervisor to inform that the address is updated. SendEmail ( address, email ); void SaveAddress ( string address ) { // Save the address. // ... } void SendEmail ( string address, string email ) { // Send an email to inform the supervisor that the address is changed. // ... }不好:
// Save address and send an email to the supervisor to inform that the address is updated. SaveAddress ( address, email ); void SaveAddress ( string address, string email ) { // Job 1. // Save the address. // ... // Job 2. // Send an email to inform the supervisor that the address is changed. // ... }
好:
int age; string name; object contactInfo;
不好:
Int16 age; String name; Object contactInfo;
好:
enum MailType { Html, PlainText, Attachment } void SendMail (string message, MailType mailType) { switch ( mailType ) { case MailType.Html: // Do something break; case MailType.PlainText: // Do something break; case MailType.Attachment: // Do something break; default: // Do something break; } } 不好:
void SendMail (string message, string mailType) { switch ( mailType ) { case "Html": // Do something break; case "PlainText": // Do something break; case "Attachment": // Do something break; default: // Do something break; } }
注釋
異常處理
好:
void ReadFromFile ( string fileName ) { try { // read from file. } catch (FileIOException ex) { // log error. // re-throw exception depending on your case. throw; } }不好: void ReadFromFile ( string fileName ) { try { // read from file. } catch (Exception ex) { // Catching general exception is bad... we will never know whether it // was a file error or some other error. // Here you are hiding an exception. // In this case no one will ever know that an exception happened. return ""; } }
作者:
RDIF
出處:
http://www.rzrgm.cn/huyong/
Email:
406590790@qq.com
QQ:
406590790
微信:
13005007127(同手機(jī)號(hào))
框架官網(wǎng):
http://www.guosisoft.com/
http://www.rdiframework.net/
框架其他博客:
http://blog.csdn.net/chinahuyong
http://www.rzrgm.cn/huyong
國(guó)思RDIF開發(fā)框架
,
給用戶和開發(fā)者最佳的.Net框架平臺(tái)方案,為企業(yè)快速構(gòu)建跨平臺(tái)、企業(yè)級(jí)的應(yīng)用提供強(qiáng)大支持。
關(guān)于作者:系統(tǒng)架構(gòu)師、信息系統(tǒng)項(xiàng)目管理師、DBA。專注于微軟平臺(tái)項(xiàng)目架構(gòu)、管理和企業(yè)解決方案,多年項(xiàng)目開發(fā)與管理經(jīng)驗(yàn),曾多次組織并開發(fā)多個(gè)大型項(xiàng)目,在面向?qū)ο蟆⒚嫦蚍?wù)以及數(shù)據(jù)庫領(lǐng)域有一定的造詣。現(xiàn)主要從事基于
RDIF
框架的技術(shù)開發(fā)、咨詢工作,主要服務(wù)于金融、醫(yī)療衛(wèi)生、鐵路、電信、物流、物聯(lián)網(wǎng)、制造、零售等行業(yè)。
如有問題或建議,請(qǐng)多多賜教!
本文版權(quán)歸作者和CNBLOGS博客共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,如有問題,可以通過微信、郵箱、QQ等聯(lián)系我,非常感謝。

浙公網(wǎng)安備 33010602011771號(hào)