{"id":90,"date":"2006-08-06T20:24:26","date_gmt":"2006-08-06T10:24:26","guid":{"rendered":"http:\/\/www.thunderguy.com\/semicolon\/2006\/08\/06\/c-sharp-documentation-comments-useless\/"},"modified":"2009-12-04T22:13:10","modified_gmt":"2009-12-04T09:13:10","slug":"c-sharp-documentation-comments-useless","status":"publish","type":"post","link":"https:\/\/thunderguy.com\/semicolon\/2006\/08\/06\/c-sharp-documentation-comments-useless\/","title":{"rendered":"C# documentation comments: useless?"},"content":{"rendered":"<p>Despite C# being several years old, there&#8217;s no official supported tool for generating human-readable documentation from C# programs. Microsoft&#8217;s C# specification describes a rather verbose form of documentation comment that programmers are supposed to use in their code, but they don&#8217;t supply the tools to do anything useful with these comments. After a bit of searching and quite a few dead ends, I finally found a simple way of generating the documenttion I needed. Here&#8217;s the full story.<\/p>\n<p>The documentation comments in C# code aren&#8217;t ignored entirely by the Microsoft C# tools. The C# compiler usefully checks the documentation comments for consistency with code, although it does seem strange for a language compiler to be looking at comments. If the correct command-line option is used, the compiler will also copy the documentation comments to an XML Documentation file, which will thus contain all the documentation comments for all files in the project. However, the XML file is useless as it stands. <a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/csref\/html\/vclrftagsfordocumentationcomments.asp\">Recommended Tags for Documentation Comments<\/a> on MSDN says, &#8220;Processing the XML file to create documentation is a detail that needs to be implemented at your site.&#8221; They call it a &#8220;detail&#8221;; I would call it the whole purpose of having these doc comments in the first place. <\/p>\n<p>Compare this with the Java world, where Javadoc has been there since the beginning (more or less). Javadoc can generate documentation directly from source code (incorporating documentation comments) or object code (without comments). Documentation processing is done entirely by the Javadoc tool; the Java compiler ignores comments, as it should (they are called &#8220;comments&#8221; after all). <\/p>\n<p>To produce usable documentation from C# documentation comments, a &#8220;documentation generator&#8221; is required to combine the object code (assembly) and the XML file into human-readable form. So the processing of documentation comments is divided between the compiler and the documentation generator, and the code must be compiled in order to produce documentation. This seems to mean that it would not be possible to produce documentation for an incomplete source hierarchy, though I haven&#8217;t tested this. <\/p>\n<p>So, we need a documentation generator. There&#8217;s lots of information in various places about the very useful &#8220;Code comment web reports&#8221; function in Visual Studio .Net (e.g. <a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/vsintro7\/html\/vxconCommentingCode.asp\">Creating Code Reports<\/a> on MSDN). Sadly, one fact that is not documented is that this feature is no longer in VS 2005. <\/p>\n<p>There is also a lot of information about the open-source NDoc tool too. Sadly, a little bit of digging reveals that it does not support .Net 2.0 and it is no longer being actively developed. The original developer has left the project, and while it has been handed over to a new team, no new development has yet surfaced. There are unofficial efforts to support .Net 2.0, such as Jonas Lagerblad&#8217;s <a href=\"http:\/\/jonas.lagerblad.com\/blog\/?p=5\">NDoc for .Net 2.0<\/a>, but they look a bit unfinished. <\/p>\n<p>Just a few days ago, Microsoft released the first public version of a documentation generator that they use internally. <a href=\"http:\/\/blogs.msdn.com\/sandcastle\/\">Sandcastle<\/a> is not (yet) an officially supported product, and the version available now is a pre-release. It&#8217;s not at all easy to set up and use, but I did manage to get it to work with the help of an <a href=\"http:\/\/blog.ljusberg.com\/2006\/07\/msbuild-script-for-sandcastle.html\">MSBuild script for Sandcastle<\/a> from Anders Ljusberg. Sandcastle ground away for ten minutes before producing the documentation for the class libraries used by my web application. <\/p>\n<p>The output of Sandcastle is a Microsoft compiled HTML (CHM) file. This shows a tree view of namespaces, which contain classes, which contain members. Each has all the basic stuff you would expect &#8212; comments, cross-references to other types, and syntax-highlighted method signatures (in your choice of C#, Visual Basic or Managed C++). Unfortunately, there&#8217;s no overall view, like class or inheritance diagrams. <\/p>\n<p>Sandcastle goes by the book, and produces its input by combining the XML Documentation file (for the comments) and the compiled code assemblies (for all other input). Producing the assemblies is no problem, but it turns out to be a real pain to generate the XML Documentation file for Visual Studio 2005 Web site projects. This is a real shame if you&#8217;re trying to generate documentation for a web site, as I was. It turns out that this is a new VS 2005 feature. The new Web site project type is a streamlined version of the old Web application project type, and one of the dropped features was the simple one-click XML Documentation file generation. <\/p>\n<p>I found a few workaround solutions for this problem. The best seemed to be to install an add-in to Visual Studio 2005 that allows a Web site project to be converted to a Web application. This turned out to be a bit of a rigmarole. First I had to install a <a href=\"http:\/\/www.microsoft.com\/downloads\/details.aspx?FamilyId=8B05EE00-9554-4733-8725-3CA89DD9BFCA&amp;displaylang=en\">Visual Studio Update to Support Web Application Projects<\/a>. I installed it &#8212; the progress indicator counted down the number of seconds remaining until it reached zero, and then I sat there looking at &#8220;Time remaining: 0 seconds&#8221; for about 15 minutes before it finally told me that the update had been installed. Amusingly, I then got another progress indicator showing &#8220;Time remaining: 8 seconds&#8221; for about half a minute before the installation actually finished. I expected the worst when I was finally able to install the actual <a href=\"http:\/\/msdn.microsoft.com\/asp.net\/reference\/infrastructure\/wap\/default.aspx\">Web Application Projects add-in<\/a>, but it only took a minute or so. <\/p>\n<p>After all that, I never even used the add-in, because in the time it took to install all this, I discovered a much better solution. <a href=\"http:\/\/www.stack.nl\/~dimitri\/doxygen\/index.html\">Doxygen<\/a> was originally a C++ documentation generator using the same sort of approach as Javadoc, except it works only on source files. I remember being quite impressed by Doxygen when I used it on a large C++ project a few years ago &#8212; the document comment style it supported was much more elegant and concise than Javadoc comments (and C# documentation comments, which are even worse). So imagine my joy when I discovered that Doxygen now supports C#, including C# style documentation comments. <\/p>\n<p>I downloaded Doxygen and ran the Doxywizard GUI, and within five minutes had complete documentation for my class libraries and website. And when I say complete, I mean all the basics, plus class inheritance graphs, collaboration diagrams, and cross-references to syntax-highlighted source code. Doxygen does this by ignoring any XML Documentation files, and simply getting all its information directly from the source files &#8212; a much better idea. <\/p>\n<p>It&#8217;s true that Doxygen&#8217;s C++ roots run deep; I don&#8217;t know how well it handles C# features like anonymous methods, delegates, events and nullable types. But certainly it has some very impressive features, and amply fulfils my needs. It still seems quite strange to me that Microsoft don&#8217;t have any support for generating proper documentation at all, let alone integrated into their IDE; but until they do (or NDoc 2.0 appears), Doxygen will do it for me. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Despite C# being several years old, there&#8217;s no official supported tool for generating human-readable documentation from C# programs. Microsoft&#8217;s C# specification describes a rather verbose form of documentation comment that programmers are supposed to use in their code, but they don&#8217;t supply the tools to do anything useful with these comments. After a bit of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[19,32],"class_list":["post-90","post","type-post","status-publish","format-standard","hentry","category-general","tag-dotnet","tag-c-sharp"],"_links":{"self":[{"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/posts\/90","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/comments?post=90"}],"version-history":[{"count":1,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/posts\/90\/revisions"}],"predecessor-version":[{"id":273,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/posts\/90\/revisions\/273"}],"wp:attachment":[{"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/media?parent=90"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/categories?post=90"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/tags?post=90"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}