命名慣例和規范
注記: 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;
}
}別把成員變量聲明為 public 或 protected。都聲明為 private 而使用 public/protected 的Properties.
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 ""; } }不必在所有方法中捕捉一般異常。不管它,讓程序崩潰。這將幫助你在開發周期發現大多數的錯誤。
原貼: http://tb.blog.csdn.net/TrackBack.aspx?PostId=66467