C#编码规范和编程好习惯(2)

C#编码规范和编程好习惯(2)

[@more@]

花括弧需独立一行,而不象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++)
   {
    //
   }
  }

良好的编程习惯

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

 
     
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 ;

}

避免使用大文件。如果一个文件里的代码超过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命名空间中定义的别名类型。

好:

 
     
None.gif498)this.style.width=498;" align="top" /> int age;

None.gif498)this.style.width=498;" align="top" /> string name;

None.gif498)this.style.width=498;" align="top" /> 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。

不在代码中使用具体的路径和驱动器名。 使用相对路径,并使路径可编程。

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7839366/viewspace-920515/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/7839366/viewspace-920515/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值