誰都會寫代碼!幾個月的編程經驗可以讓你寫出“可運行應用程序”。讓它可運行容易,但是以最有效率的方式編碼就需要下更多的功夫!
要知道,大多數程序員在寫”可運行代碼,“而不是”高效代碼“。我們在這個指南課程前面提到,你想成為你們公司”最尊貴的專業人員“嗎?寫”高效代碼“是一項藝術,你必須學習和實踐它。
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 ""; } }新聞熱點
疑難解答