.Net最佳实践 (23-32)

 

  1. Have your own templates for each of the file types in Visual Studio. You can include your company name (such as HP) , copy right information etc in the template. You can view or edit the Vis ual Studio file templates in the folder C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\ItemTemplatesCache\CSharp\1033 . (This folder has the templates for C#, but you can easily find the corresponding folders or any other language)


23. Visual Studio 中,针对每个文件类型创建自己的模板。你可以 在模板中 包含你的公司名称(例如 HP ), 版权信息等等。 你可以在这个文件夹下查看或者编辑 Visual Studio 中的文件模板: C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\ItemTemplatesCache\CSharp\1033 。(这个文件夹有 C# 的模板, 但是你可以容易的找到其他语言对应的文件夹。

 

  1. Avoid having very large files. If a single file h as more than 1000 lines of code, it is a good candidate for refactoring. Split them logically into two or more classes.


24. 避免存在非常大 的文件。如果单个文件有超过 1000 行的代码,那这就是个 需要 重构 的很好的例子。从逻辑上将它们 拆分两个或者多个类。

 

  1. Avoid public methods and properties, unless they really need to be a ccessed from outside the class. Use “internal” if they are accessed only within the same assembly.


25. 避免公开的方法和属性,除非他们真正需要从 这个类的外部来访问。如果他们 只需要在同一个 assembly 里面访问的话,使用 internal

 

  1. Avoid passing too many parameters to a method. If you have more than 4~5 parameter s, it is a good candidate to define a class or structure.


26. 避免传递太多的参数到一个方法中。如果你有大于 4 5 个参数, 这就是一个很好的例子来定义一个类或者结果。

 

  1. If you have a method returning a collection, return an empty collection instead of null, if you have no data to return. For example, if you ha ve a method returning an ArrayList, always return a valid ArrayList. If you have no items to return, then return a valid ArrayList with 0 items. This will make it easy for the calling application to just check for the “count” rather than doing an additional check for “null”.


27. 如果你有一个方法返回 一个集合,没有数据 返回的时候 返回空集合而不要返回 null 。例如,如果你有一个方法返回一个 ArrayList ,总是要返回一个有效的 ArrayList 。如果你没有数据返回 ,那么就返回一个包含 0 条数据的 ArrayList 。这样对 调用的应用程序很容易,只需要检查 条数 而不需要对 “null” 进行额外的检查。

 

  1. Use the AssemblyInfo file to fill information like version number, description, company name, copyright notice etc.


28. 使用 AssemblyInfo 文件来填写信息,录入版本号,描述,公司名称,版权通知等。

 

  1. Logically organize all your files within appropriate folders. Use 2 level folder hierarchies. You can have up to 10 folders in the root folder and each folder can have up to 5 sub folder s. If you have too many folders than cannot be accommodated with the above mentioned 2 level hierarchy, you may need re factoring into multiple assemblies.


2 9. 按照逻辑的方式在合适的文件夹中组织你的文件。使用两级的文件夹 组织结构。你可以 在根目录下 有最多 10 个文件夹 ,每个文件夹有最多 5 个子文件夹。如果你有太多的 文件夹而不能放到上面提到的两级组织 结构中,你可能需要重构到多个 Assembiy 中。

 

  1. Make sure you have a good logging class which can be configured to log errors, warning or traces. If you configure to log errors, it should only log errors. But if you configure to log traces, it should record all (errors, warnings an d trace). Your log class should be written such a way that in future you can change it easily to log to Windows Event Log, SQL Server, or Email to administrator or to a File etc without any change in any other part of the application. Use the log class extensively throughout the code to record errors, warning and even trace messages that can help you trouble shoot a problem.


30. 保证你有一个很好的日志 类,配置后可以用来记录错误、警告或者 跟踪 如果你配置成记录错误,它应该只记录错误。但是如果你配置 成跟踪,它应该记录所有的(错误、警告和跟踪)。你的日志类应该 写成 这样 的方式 :在将来 不需要修改应用程序的任何其他部分, 可以 通过 简单的修改 将日志记录到 Windows 事件日志、 SQL Server ,或者给管理员发邮件, 或者记录到一个文件等。

 

  1. If you are opening database connections, sockets, file stream etc, always cl ose them in the finally block. This will ensure that even if an exception occurs after opening the connection, it will be safely closed in the finally block. If it implements the IDisposable interface, please include the object in the using ( ).


31. 如果 你正在打开数据库连接,套接字,文件流等等,总是要在 finally 代码块里面关闭 们。 这将确保 在打开连接以后即使有异常发生也可以在 finally 代码块中安全 的关闭它们。如果使用了 Idisposable 接口,请包含这个对象到 using ()中。

 

  1. Use StringBuilder class instead of String when you have to manipulate string objects in a loop. The String object works in weird way in .NET . Each time you append a string, it is actually discarding the old string object and recreating a new object, which is a relatively expensive operations.

 

32. 当你不得不 在一个循环中操作字符串对象的时候 使用 StringBuilder 类代替 String String 对象在 .Net 中以一种 怪异的方式工作。每次你追加一个 字符串,它实际上抛弃了老的字符串对象而重新创建一个新的对象, 这样的方式是一个相对昂贵的操作。

 

Consider the following example:


考虑 下面的例子:

 

public string ComposeMessage (string[] lines)

{

      string message = String.Empty;

 

      for (int i = 0; i < lines.Length; i++)

      {

            message += lines [i];

      }

 

    return message;

}

 

In the above example, it may look like we are just appending to the string object ‘message’. But what is happening in reality is, the string object is discarded in each iteration and recreated and appending the line to it.

 

在上面的例子中, 看上去好像我们只是添加到字符串对象 “messa ge” 。但是事实上发生的是,这个字符串对象 在每个迭代中都会 被抛弃 被重新创建 ,然后添加一行到这个对象里面。

 

If your loop has several iterations, then it is a good idea to use StringBuilder class instead of String object.

 

如果你的循环有若干个迭代,那么使用 StringBuilder 类代替 String 对象是个好主意。


See the example where the String object is replaced with StringBuilder.

 

看下面这个例子,其中 String 对象被替换成了 StringBuffer

 

public string ComposeMessage (string[] lines)

{

        StringBuilder message = new StringBuilder();

 

        for (int i = 0; i < lines.Length; i++)

        {

            message.Append( lines[i] );

        }

 

        return message.ToString();

     }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值