【导读】本文介绍在C# 编码的时候需要遵守的规范,以及程序员应该养成的编程好习惯…… 
 
 
命名惯例和规范

注记: Pascal 大小写形式-所有单词第一个字母大写,其他字母小写。

Camel大小写形式-除了第一个单词,所有单词第一个字母大写,其他字母小写。



类名使用Pascal 大小写形式 public class HelloWorld
{ ...
}
 

方法使用Pascal 大小写形式 public class HelloWorld
{
 void SayHello(string name)
 {
  ...
 }
}< 

变量和方法参数使用Camel 大小写形式 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) 
缩进和间隔
缩进用 TAB . 不用 SPACES.。 
注释需和代码对齐.。 
花括弧 ( {}  需和括号外的代码对齐.。 
用一个空行来分开代码的逻辑分组。. 

 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, for 等可以跟括号在同一行。 

好:  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++)
   {
    //
   }
  }
 
良好的编程习惯  



遵从以下良好的习惯以写出好程序


避免使用大文件。如果一个文件里的代码超过300~400行,必须考虑将代码分开到不同类中。 
避免写太长的方法。一个典型的方法代码在1~25行之间。如果一个方法发代码超过25行,应该考虑将其分解为不同的方法。 
方法名需能看出它作什么。别使用会引起误解的名字。如果名字一目了然,就无需用文档来解释方法的功能了。

好:  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.
  // ...
 }
 

使用C# 或 VB.NET的特有类型,而不是System命名空间中定义的别名类型。

好:  int age;
 string name;
 object contactInfo;


不好: 
 Int16 age;
 String name;
 Object contactInfo;
 

别在程序中使用固定数值,用常量代替。 
别用字符串常数。用资源文件。 
避免使用很多成员变量。声明局部变量,并传递给方法。不要在方法间共享成员变量。如果在几个方法间共享一个成员变量,那就很难知道是哪个方法在什么时候修改了它的值。 
必要时使用enum 。别用数字或字符串来指示离散值。 

好:  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. 

不在代码中使用具体的路径和驱动器名。 使用相对路径,并使路径可编程。 
永远别设想你的代码是在“C:”盘运行。你不会知道,一些用户在网络或“Z:”盘运行程序。 
应用程序启动时作些“自检”并确保所需文件和附件在指定的位置。必要时检查数据库连接。出现任何问题给用户一个友好的提示。 
如果需要的配置文件找不到,应用程序需能自己创建使用默认值的一份。 
如果在配置文件中发现错误值,应用程序要抛出错误,给出提示消息告诉用户正确值。 
错误消息需能帮助用户解决问题。永远别用象"应用程序出错", "发现一个错误" 等错误消息。而应给出象 "更新数据库失败。请确保登陆id和密码正确。" 的具体消息。 ? 
显示错误消息时,除了说哪里错了,还应提示用户如何解决问题。不要用 象 "更新数据库失败。"这样的,要提示用户怎么做:"更新数据库失败。请确保登陆id和密码正确。" 
显示给用户的消息要简短而友好。但要把所有可能的信息都记录下来,以助诊断问题。 
注释
别每行代码,每个声明的变量都做注释。 
在需要的地方注释。可读性强的代码需要很少的注释。如果所有的变量和方法的命名都很有意义,会使代码可读性很强并无需太多注释。 
行数不多的注释会使代码看起来优雅。但如果代码不清晰,可读性差,那就糟糕。 
如果应为某种原因使用了复杂艰涩的原理,为程序配备良好的文档和重分的注释。 
对一个数值变量采用不是0,-1等的数值初始化,给出选择该值的理由。 
简言之,要写清晰,可读的代码以致无须什么注释就能理解。 
对注释做拼写检查,保证语法和标点符号的正确使用。


异常处理
不要“捕捉了异常却什么也不做“。如果隐藏了一个异常,你将永远不知道异常到底发生了没有。 
发生异常时,给出友好的消息给用户,但要精确记录错误的所有可能细节,包括发生的时间,和相关方法,类名等。 
只捕捉特定的异常,而不是一般的异常。

好:  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 "";  
  }
 }
 
不必在所有方法中捕捉一般异常。不管它,让程序崩溃。这将帮助你在开发周期发现大多数的错误。 
你可以用应用程序级(线程级)错误处理器处理所有一般的异常。遇到”以外的一般性错误“时,此错误处理器应该捕捉异常,给用户提示消息,在应用程序关闭或 用户选择”忽略并继续“之前记录错误信息。 
不必每个方法都用try-catch。当特定的异常可能发生时才使用。比如,当你写文件时,处理异常FileIOException. 
别写太大的 try-catch 模块。如果需要,为每个执行的任务编写单独的 try-catch 模块。 这将帮你找出哪一段代码产生异常,并给用户发出特定的错误消息 
如果应用程序需要,可以编写自己的异常类。自定义异常不应从基类SystemException派生,而要继承于. IApplicationException。  

posted on 2007-02-27 09:58  Dragon-China  阅读(382)  评论(0编辑  收藏  举报