简体   繁体   English

使用XML注释记录C#代码的最佳实践是什么?

[英]What Are Best Practices For Documenting C# code with XML comments?

I'm going through some new code I just wrote and adding NDoc sytle comments to my classes and methods. 我正在编写一些我刚编写的新代码,并将NDoc sytle注释添加到我的类和方法中。 I'm hoping to generate a pretty good MSDN style document for reference. 我希望生成一个非常好的MSDN样式文档供参考。

In general, what are some good guidelines when writing comments for a class and for a method? 一般来说,在为类和方法编写注释时,有哪些好的指导原则? What should the NDoc comments say? NDoc评论应该说什么? What should they not say? 他们应该怎么说?

I find myself looking at what the .NET framework comments say, but that gets old fast; 我发现自己正在研究.NET框架评论所说的内容,但这种情况会变得很快; if I could have some good rules to guide myself, I could finish my docs a lot faster. 如果我能有一些好的规则来指导自己,我可以更快地完成我的文档。

In comments used to build API documentation, you should: 在用于构建API文档的注释中,您应该:

  • Explain what the method or property does, why it exists at all, and explain any domain concepts that are not self-evident to the average consumer of your code. 解释方法或属性的作用,为什么它存在,并解释任何对代码的普通消费者不言自明的域概念。

  • List all preconditions for your parameters (cannot be null, must be within a certain range, etc.) 列出参数的所有前提条件(不能为空,必须在一定范围内等)

  • List any postconditions that could influence how callers deal with return values. 列出可能影响调用者处理返回值的任何后置条件。

  • List any exceptions the method may throw (and under what circumstances). 列出方法可能抛出的任何异常(以及在什么情况下)。

  • If similar methods exist, explain the differences between them. 如果存在类似的方法,请解释它们之间的差异。

  • Call attention to anything unexpected (such as modifying global state). 注意任何意外的事情(例如修改全局状态)。

  • Enumerate any side-effects, if there are any. 列举任何副作用,如果有的话。

If you end up with comments that don't add any value, they're just wasteful. 如果您最终得到的评论不会增加任何价值,那么它们就是浪费。

For example 例如

/// <summary>
/// Gets manager approval for an action
/// </summary>
/// <param name="action">An action object to get approval for</param>
public void GetManagerApprovalFor(Action action)

...you added absolutely no value and just added more code to maintain. ...你绝对没有添加任何价值,只是添加了更多代码来维护。

Too often code is littered with these superfluous comments. 很多代码都充斥着这些多余的评论。

StyleCop provides hints for code and commenting style. StyleCop提供代码注释样式的提示。 The suggestions it gives are in line with the MSDN documentation style. 它提供的建议符合MSDN文档样式。

As for the contents of the comment, it should give the user of your code enough information on what kind of behavior to expect. 至于评论的内容,它应该为您的代码的用户提供足够的信息,说明期望的行为类型。 It should also answer potential questions the user might have. 它还应该回答用户可能遇到的潜在问题。 So try to use your code as someone who doesn't know anything about the code, or even better, ask someone else to do so. 因此,尝试将您的代码用作对代码一无所知的人,或者甚至更好,请其他人这样做。

For properties, your comment should indicate whether the property is read only, write only or read write. 对于属性,您的注释应指示属性是只读,只写还是读写。 If you look at all official MS documentation, property doc comments always start with "Gets ...", "Gets or sets..." and (very rarely, avoid write only properties usually) "Sets ..." 如果你查看所有正式的MS文档,属性文档注释总是以“获取...”,“获取或设置...”开头,并且(很少,通常避免只写属性)“设置......”

Don't forget what's a valid XML is. 不要忘记什么是有效的XML。 For example: 例如:

/// <Summary>
/// Triggers an event if number of users > 1000
/// </Summary>

(Error: invalid XML). (错误:XML无效)。

I write the <summary> comment to include the information I would want to know if I was the one calling that function (or instantiating that class). 我编写<summary>注释以包含我想知道的信息,如果我是调用该函数的那个​​(或实例化该类)。

I write the <remarks> comment to include information I would want to know if I was tasked with debugging or enhancing that function or class. 我编写<remarks>注释以包含我想要知道的信息,如果我负责调试或增强该函数或类。 Note: this doesn't replace the need for good inline comments. 注意:这并不能取代良好的内联注释。 But sometimes a general overview of the inner workings of the function/class are very helpful. 但有时候对函数/类的内部工作原理的一般概述非常有用。

As stated on the MSDN page , you use XML documentation comments to generate documentation automatically, so it makers sense if you're writing an API and don't want to work twice at both code and documentation, with the added benefit of keeping them in sync - every time you change the code, you modify the appropriate comments and regenerate the docs. 正如MSDN页面上所述,您使用XML文档注释自动生成文档,因此制造商会感觉您是否正在编写API并且不希望在代码和文档中都使用两次,并且还可以将它们保存在同步 - 每次更改代码时,都会修改相应的注释并重新生成文档。

Compile with /doc and the compiler will search for all XML tags in the source code and create an XML documentation file, then use a tool such as Sandcastle to generate the full docs. 使用/doc进行编译,编译器将在源代码中搜索所有XML标记并创建XML文档文件,然后使用Sandcastle等工具生成完整的文档。

One thing about comments is UPDATING them. 关于评论的一点是更新它们。 Too many people alter a function then don't change the comment to reflect the change. 太多人改变了一个功能,然后不改变评论以反映变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM