谁都会写代码!几个月的编程经验可以让你写出“可运行应用程序”。让它可运行容易,但是以最有效率的方式编码就需要下更多的功夫!
要知道,大多数程序员在写”可运行代码,“而不是”高效代码“。我们在这个指南课程前面提到,你想成为你们公司”最尊贵的专业人员“吗?写”高效代码“是一项艺术,你必须学习和实践它。
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 ""; } }
新闻热点
疑难解答