誰都會寫代碼!幾個月的編程經驗可以讓你寫出“可運行應用程序”。讓它可運行容易,但是以最有效率的方式編碼就需要下更多的功夫!
要知道,大多數程序員在寫”可運行代碼,“而不是”高效代碼“。我們在這個指南課程前面提到,你想成為你們公司”最尊貴的專業人員“嗎?寫”高效代碼“是一項藝術,你必須學習和實踐它。
注記 :
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; ... }}
string m_sName;int nAge;然而,這種方式在.Net編碼規范中是不推薦的。所有變量都用camel 大小寫形式,而不是用數據類型和m_來作前綴。
for ( int i = 0; i < count; i++ ){ ...}如果變量只用於迭代計數,沒有在循環的其他地方出現,許多人還是喜歡用單個字母的變量(i) ,而不是另外取名。
. . .
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 ""; } }