views:

644

answers:

2

NDoc has an XML element inheritdoc which allows you to inherit documentation of a member from the parent class (or an implemented interface). However, Visual Studio (i.e. the C# compiler) does not understand this tag and complains about the documentation not being present or complete. So does StyleCop and some other tools. Is there an alternative approach? How do you go about keeping the docs complete, yet without duplicating the XML descriptions?

A: 

One alternative is to use GhostDoc - an add-in for Visual Studio that automatically generates comments for you. This duplicates the XML description of course, which is part of what you're trying to avoid - but at least it does it automatically for you.

What happens if you just leave off the docs entirely for methods which are being inherited, or overriding interface methods? I suspect it depends on how you've got NDoc configured, but certainly in MSDN documentation seems to just naturally inherit the docs - and a quick check suggests that VS won't whinge when you don't prodive docs for an inherited method. Worth a try, certainly.

Jon Skeet
Thanks for your reply. If you leave out the comments, NDoc inherits the docs correctly, but VS still complains about doc comments being missing, which is kind of annoying. GhostDoc is a nice tool. ReSharper has also a related feature - Copy comments from base.
petr k.
comment generators produce code smell
Pavel Savara
+4  A: 

I have a better answer: FiXml.

Cloning comments with GhostDoc is certainly working approach, but it has significant disadvantages, e.g.:

  • When the original comment is changed (which frequently happens during development), its clone is not.
  • You're producing huge amount of duplicates. If you're using any source code analysis tools (e.g. Duplicate Finder in Team City), it will find mainly your comments.

Short description of FiXml: it is a post-processor of XML documentation produced by C# \ Visual Basic .Net. It is implemented as MSBuild task, so it's quite easy to integrate it to any project. It addresses few annoying cases related to writing XML documentation in these languages:

  • No support for inheriting the documentation from base class or interface. I.e. a documentation for any overridden member should be written from scratch, although normally it’s quite desirable to inherit at least the part of it.
  • No support for insertion of commonly used documentation templates, such as “This type is singleton - use its <see cref="Instance" /> property to get the only instance of it.”, or even “Initializes a new instance of <CurrentType> class.”

To solve mentioned issues, the following additional XML tags are provided:

  • <inheritdoc />, <inherited /> tags
  • <see cref="..." copy="..." /> attribute in <see/> tag.

Here is its web page and download page.

Finally, there is <inheritdoc> tag in Sandcastle - it's definitely better to use it than to copy XML comments, but it has few disadvantages in comparison to FiXml:

  • Sandcastle produces compiled HTML help files - it doesn't modify .xml files containing extracted XML comments. But these files are used by many tools, including .NET Reflector and class browser \ IntelliSense in Visual Studio .NET. So if you use just Sandcastle, you won't see inherited documentation there.
  • Sandcastle's implementation is less powerful. E.g. the is no <see ... copy="true" />.

See Sandcastle's <inheritdoc> description for further details.

Alex Yakunin