撰文:DotNetSpider.com(http://www.dotnetspider.com)
翻譯:楊賀宏
誰都會寫代碼!幾個月的編程經驗可以讓你寫出“可運行應用程序”。讓它可運行容易,但是以最有效率的方式編碼就需要下更多的功夫!
要知道,大多數程序員在寫”可運行代碼,“而不是”高效代碼“。我們在這個指南課程前面提到,你想成為你們公司”最尊貴的專業人員“嗎?寫”高效代碼“是一項藝術,你必須學習和實踐它。
命名慣例和規范
注記 :
Pascal 大小寫形式-所有單詞第一個字母大寫,其他字母小寫。
Camel 大小寫形式-除了第一個單詞,所有單詞第一個字母大寫,其他字母小寫。
public class HelloWorld{ ...}
public class HelloWorld{ void SayHello(string name) { ... }}
public class HelloWorld{ int totalCount = 0; void SayHello(string name) { string fullMessage = "Hello " + name; ... }}
以前,多數程序員喜歡它-把數據類型作為變量名的前綴而m_作為成員變量的前綴。例如:
string m_sName;int nAge;然而,這種方式在.NET編碼規范中是不推薦的。所有變量都用camel 大小寫形式,而不是用數據類型和m_來作前綴。
- 別用縮寫。用name, address, salary等代替 nam, addr, sal
- 別使用單個字母的變量象i, n, x 等. 使用 index, temp等
用于循環迭代的變量例外:
for ( int i = 0; i < count; i++ ){ ...}如果變量只用于迭代計數,沒有在循環的其他地方出現,許多人還是喜歡用單個字母的變量(i) ,而不是另外取名。
- 變量名中不使用下劃線 (_) 。
- 命名空間需按照標準的模式命名
. . .
例如,對于類HelloWorld, 相應的文件名應為 helloworld.cs (或, helloworld.vb)
縮進和間隔
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++) { // } }
良好的編程習慣
遵從以下良好的習慣以寫出好程序
好:
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; } }
以前,多數程序員喜歡它-把數據類型作為變量名的前綴而m_作為成員變量的前綴。例如:
string m_sName;int nAge;然而,這種方式在.NET編碼規范中是不推薦的。所有變量都用camel 大小寫形式,而不是用數據類型和m_來作前綴。
- 別用縮寫。用name, address, salary等代替 nam, addr, sal
- 別使用單個字母的變量象i, n, x 等. 使用 index, temp等
用于循環迭代的變量例外:
for ( int i = 0; i < count; i++ ){ ...}如果變量只用于迭代計數,沒有在循環的其他地方出現,許多人還是喜歡用單個字母的變量(i) ,而不是另外取名。
- 變量名中不使用下劃線 (_) 。
- 命名空間需按照標準的模式命名
. . .
例如,對于類HelloWorld, 相應的文件名應為 helloworld.cs (或, helloworld.vb)
縮進和間隔
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++) { // } }
良好的編程習慣
遵從以下良好的習慣以寫出好程序
好:
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 ""; } }
注釋
異常處理
好:
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 ""; } }