.Net最佳实践 (1-10)

.Net最佳实践

2009年8月30日

对天发誓,这篇文章绝对是我刚才翻译的

译者注:弟弟刚才叫我帮他翻译一篇文章,我哪敢不从,遂译之。翻译的过程中发现很有道理,对大多数编程语言都适用,翻译下来,学了东西不少,深感浑身舒服,遂共享之。欢迎大家拍砖。

到Twitter上follow译者: @jiakuan
到新浪微薄上专注译者: http://t.sina.com.cn/jiakuan


正片开始上映:
------------------------------------------------------------

  1. Avoid writing very long methods. A method should typically have 1~25 lines of code. If a method has more than 25 lines of code, you must consider re factoring into separate methods.


1.  避免编写非常长的方法。典型的情况下一个方法应该只有 1 25 代码。如果一个方法含有多余 25 行的代码,你必须考虑重构到 单独的方法中。


  1. A m ethod should do only 'one job'. Do not combine more than one job in a single method, even if those jobs are very small.


2.   一个方法应该只做 一个 任务 即使任务都非常小,也不要把多个任务 一起放到单个方法中。


Good:

              // 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 )

              {

                            // Se nd an email to inform the supervisor that the address is changed.

                            // ...

              }

 

Not Good:

 

              // Save address and send an email to the supervisor to inform that

// the address is updated.

// 保存地址并发送一个邮件 给管理员,告知这个地址被更新了。

              SaveAddress ( address, email );

 

              voi d SaveAddress ( string address, string email )

              {

                            // Job 1.

                            // Save the address.

                            // ...

                            // 任务 1

                            // 保存这个地址。

                            //

 

                            // Job 2.

                            // Send an email to inform the supervisor that the address is changed.

                            // ...

                            // 任务 2

                            // 发送一个邮件告知管理员这个地址被更新了。

              }

 

  1. Use the c# or VB.NET specific types (aliases), rather than the types defined in System namespace.


3.  使用 C# 或者 VB.NET 的特定类型(别名), 而不是用在 System 名字空间里面定义的类型。

 

              int age;   (not I nt 16 )

              string name;  (not S tring)

              object contactInfo; (not O bject)

 

             

Some devel opers prefer to use types in Common Type System than language specific aliases.

相对于语言 特定的别名, 有些开发人员 喜欢使用 Common Type System 里面的类型

 

  1. Always watch for unexpected values. For example, if you are using a parameter with 2 possible values, never assume that if on e is not matching then the only possibility is the other value.


4. 总是检查事先不能预料的值 。例如,如果你正在 使用的一个参数有两个可能的值,绝对不要假设 如果其中一个值不匹配就一定会是另一个值。

 

Good:

 

If ( memberType == eMemberTypes.Registered )

{

              // registered user… do something…

// 注册 用户 做相关处理

}

else if ( memberTy pe == eMemberTypes.Guest )

{

              // Guest user... do something…
// 游客 做相关处理

}

else

{

                            // Unexpected user type. Throw an exception

                            // 未知的用户类型,抛出 一个异常

                            Throw new Exception (“Un expected value “ + memberType.ToString() + “’.”)

 

                            // If we introduce a new us er type in future, we can easily find

// the problem here.

// 如果我们将来引入一个新的用户类型,我们就可以容易的从这个地方发现

// 问题。

}

 

Not Good:

 

If ( memberType == eMemberTypes.Registered )

{

                            // Registered user… do something…

                            // 注册用户 做相关处理

}

else

{

                            // Guest user... do somethi ng…

                            // 游客 做相关处理

 

// If we introduce another user type in future, this code will

// fail and will not be noticed.

// 如果我们将来引入一个新的用户类型, 这个代码将会失败而且不会有提示。

}

 

  1. Do not hardcode numbers. Use constants instead. Declare constant in the top of the file and use it in your code.

 

However, using constants are also not recommended. You should use the constants in the config uration file or database so that you can change it later. Declare them as constants only if you are sure this value will never need to be changed.


5. 不要硬编码数字。使用 量代替。在文件的顶端声明 常量然后在你的代码中使用它。

 

尽管如此 ,使用常量也不是推荐的做法。你应该在配置文件或者数据库中使用常量,那样的话,你就可以在 之后修改它。只有在你确信这个值永远 不需要修改的时候,就可以将它们声明为常量。

 

  1. Do not hardcode strings. Use resource files.


6. 不要硬编码字符串。使用资源文件。

 

  1. Convert strings to lowercase or upper case before comparing. Th is will ensure the string will match even if the string being compared has a different case.


7. 在比较之前将字符串转换成小写或者大写。这将 保证 同时 包含大小写的字符串 在比较的时候仍然可以匹配。

 

if ( name.ToLower() == “john” )

{

                  //…

}

 

  1. Use String.Empty instead of “”


8.   使用 String.Empty ,而不是用 “”


Good:

 

If ( name == String.Empty )

{

              // do something

}

 

Not Good:

 

If ( name == “” )

{

              // do something

}

 

 

  1. Avoid using member variables. Declare local variables wherever necessary and pass it to other methods instead of sharing a member variable between methods. If you share a member variable between methods, it will be difficult to track which method changed the value and when.


9.  避免使用成员变量。在任何必要的情况下 声明局部变量 ,然后传给其他方法,而不要在方法 之间 共用 一个成员变量。 如果你在方法之间共用了一个成员变量, 将很难跟踪哪个方法修改了这个变量,在何时修改的。

 

  1. Use enum wherever required. Do not use numbers or strings to indicate discrete values.


10. 在任何需要的情况下使用枚举类型。 不要使用数字和字符串来 指定 不连续的值。

 

Good:

              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

                        

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值