上一篇 | 下一篇

C# 编码规范和编程好习惯

发布: 2008-6-30 09:25 | 作者: admin | 来源: | 查看: 2次

  谁都会写代码!几个月的编程经验可以让你写出“可运行应用程序”。让它可运行容易,但是以最有效率的方式编码就需要下更多的功夫!

  要知道,大多数程序员在写“可运行代码,而不是高效代码”。我们在这个指南课程前面提到,你想成为你们公司“最尊贵的专业人员”吗?写“高效代码”是一项艺术,你必须学习和实践它。

  命名惯例和规范  

  注记 :

  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; } }

字号: | 推荐给好友

21/212>

评分:0

我来说两句