谁都会写代码!几个月的编程经验可以让你写出“可运行应用程序”。让它可运行容易,但是以最有效率的方式编码就需要下更多的功夫!
要知道,大多数程序员在写”可运行代码,“而不是”高效代码“。我们在这个指南课程前面提到,你想成为你们公司”最尊贵的专业人员“吗?写”高效代码“是一项艺术,你必须学习和实践它。
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 ""; } }
欢迎访问最专业的网吧论坛,无盘论坛,网吧经营,网咖管理,网吧专业论坛
https://bbs.txwb.com
关注天下网吧微信/下载天下网吧APP/天下网吧小程序,一起来超精彩
|
本文来源:vczx 作者:佚名