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