views:

339

answers:

6

I can't understand the virtues of using XML comments. I know they can be converted into nice documentation external to the code, but the same can be achieved with the much more concise DOxygen syntax. In my opinion the XML comments are wrong, because:

  1. They obfuscate the comments and the code in general. (They are more difficult to read by humans).
  2. Less code can be viewed on a single screen, because "summary" and "/summary" take additional lines.
  3. (removed)

What then could have been be the reasons, why XML was preferred in .NET rather that the simple DOxygen syntax?

+10  A: 
  1. The ide picks up the comments and shows them when using that method.
  2. Everyone who programs C# is probably familiar with the XML commenting system. There's less to learn for a new hire.

I'm not saying that DOxygen isn't better, it's just that the xml commenting system is more familiar to everyone, and that goes a long way. It's just one less thing you have to train a new hire to do.

As far as leaving variables uncommented. What may be obvious to you, won't be to someone else (or to you 6 months later).

Ok now I think I see what you are asking.

  1. Obfuscating comments. The color coding helps. Personally, I quickly scan past the grey text and only read what's green unless I need to read the xml text. (in my settings at least).

  2. We have large monitors so we get more code on the screen in general. (It's cheaper to buy a large monitor than to retrain people generally). The other thing about this too, is that I bet you are only actively looking at one function at a time, so if that entire function fits on a page, you probably aren't suffering too much from not seeing more code. Now if the functions are long, then I could see that being a problem.

  3. We put the summary comments on a single line when possible (assuming it isn't really large). That cuts down on the used space.

  4. I don't know if DOxygen does this, but you can collapse the comments so they are out of the way.

Kevin
(1) The IDE could also be made to pick up DOxygen-like comments. (2) I am asking about technical comparison of two methods of commenting, not about comparison of related business decisions. Thus, I find the two points missing the point.
Michal Czardybon
I completely fail to see what you are asking then. Commenting has nothing to do with how the code runs. It's purely a business decision about how to communicate to other programmers and yourself about what the code is going to do.
Kevin
I like your answers, Kevin, but I disagree with your assessment that "It's purely a business decision". Although managers are more likely to choose a path with more scaling losses (familiarity) over one with more overhead (something new), it is IMHO a bad business choice. Regardless, it's pretty clear (to me anyway) that Michal was looking for technical reasons such as: How does one help me communicate better than the other? or Which one will help me do my job better than the other?
Joseph Gordon
I guess that is what my hang up. To me those are business decisions and not technical ones. A technical decision to me is, we use x because it increases throughput during this scenario. I don't totally disagree with you that it might be a bad business choice to pick one over the other, based solely on familiarity, but you do have to weigh the benefit vs the cost. A lot of people quite possibly don't see the advantage of changing from one to another, there is a lot of overhead in change, and sometimes ok is good enough when compared to the cost of going to something better.
Kevin
+3  A: 

what I find even more mindblowing is the popularity of the ghostdoc plugin. If you can automatically generate a comment based on a method name, why have the comment at all?

Steve Yegge says that over commenting is the sign of a newbie programmer, I have a hard time disagreeing with him.

Matt Briggs
This isn't about over-commenting - it's about specific usage of the XML comments, which are primarily for documenting code explicitly. If you rely on the method name, you don't include semantics which means future maintenance can change the sense of the method and invalidate the meaning in the documentation. Commenting the method through XML comments aids in avoiding this while also providing documentation for use in intellisense and elsewhere.
Jeff Yates
Good point in my opinion, but I cannot accept this answer, because I was asking about opposite opinions.
Michal Czardybon
@Jeff Yates: I strongly disagree. If you change the meaning, you HAVE to change the name at the same time. Proper method names are much more important than comments.
Michal Czardybon
@Michal: People just don't always do that and not every situation can be neatly explained in a method name, it just can't. Providing XML comments ensures that the method purpose is clearly defined where it is most necessary to do so.
Jeff Yates
@Matt: The comment existing even though it can be auto-generated is because of intellisense. Having some documentation appear about a method when using it provides confidence that you are using it correctly. Without it, you are guessing and though you may be guessing correctly, you cannot know for certain that you are properly interpreting the authors intent. Intellisense (and other API documentation) helps with that and XML comments help with the generation of such documentation.
Jeff Yates
I'm an avid user of ghost doc and strive to write code that requires as few comments as possible. Once you become familiar with ghost doc you can write method names that ghost doc can turn into proper sentences for the descriptions that display on intellisense. For some methods this is enough otherwise I will add information or change some of the generated text but it's great that it renders out all of the parameters etc in correct xml documentation format.
Chris Marisic
@Jeff: For example, If I have a method "UpdateName(string newName)", how is "Updates the name" in a comment summary going to help anyone understand what is going on? It adds 3 lines of noise, that is all. I am not saying there is never a case for comments, what I am saying is they should only exist when there is a failure on the part of the developer to come up with a properly descriptive name. If they aren't adding anything, they shouldn't be there.
Matt Briggs
@Matt: Of course bad comments are noise. There's no excuse for that, but just because a comment can be auto-generated doesn't make it noise. The comment must provide the appropriate detail regardless. If you just use ghostdoc to create the bare minimum, then you're failing to do the job correctly.
Jeff Yates
+1  A: 

You don't have to use them in your projects.

They are a standard that happens to be integrated into Visual Studio and if you use StyleCop they can be enforced. So this is the virtue here.

However, if you decide you want to use DOxygen then there's nothing stopping you. Just make sure you are consistent.

ChrisF
+5  A: 

The primary job of XML documentation is not to generate documentation. It is to provide good IntelliSense info for the client of your class. Ship the generated .xml file along with your assembly.

Hans Passant
Good point about the IntelliSense!
code4life
+4  A: 

The virtues of using XML comments in .NET

They are natively supported by the C# compiler and Visual Studio, providing a single location to document your API for use in printed, online, and intellisense documentation.

This article from MSDN magazine states the following:

In every project, there is someone who is not happy with the documentation. The team leads want more comments in the source, technical writers want more written information about the code design, Quality Assurance wants to see functional specifications, and so on. If all of those documents are actually written, you still have the battle of keeping all of them synchronized.

While the format is not necessarily ideal, XML documentation comments provide a rich syntax such that this can be achieved.

Why not support DOxygen in C# instead?

As for why the existing XML system was chosen over DOxygen, I would suspect this is primarily because DOxygen is released under the GPL which would mean Visual Studio and the C# compiler would also need to be released as such - something that Microsoft would no doubt not want to do considering the terms of the GPL.

Jeff Yates
GPL, as it happens, is evil.
WCWedin
+1  A: 

There is not really a correct answer here imo. Neither system is "better" than the other in reality - they both do the same job in the end, which is allow you to generate code documentation.

The final output can be formatted in exactly the same way for each of them, and they do have pretty much the same functionality in terms of what labels etc they support, so its really down to personal choice here.

Personally I find XML comments to be much more human readable, much more logical and just plain easy to use - but that is with the added advantage of having visual studio automatically generate stubs for me to just fill in, and the excellent support it has for collapsing them so they don't take up lots of space on the screen. I am sure that someone who comes from a background editing in VI or some_other_IDE will have a different opinion, but there is no real advantage to either.

So I would say that really it depends on what IDE you are using and what you and your team are used to using.

Now if you are asking why Microsoft have chosen to integrate so tightly with XML commenting within Visual Studio, that is a different question. Most probably it is due to the facts that: it would be simpler for them to implement within VS (as they can re-use existing code to generate/read the comments and build intellisense etc), they have a trend for sticking to "standards" anyway (be it their own or industry ones), and also licensing reasons as mentioned by Jeff.

Just to add that the product Microsoft is using within VS is called "Sandcastle", which is an in-house XML doc generation tool. It has its own wiki page @ http://docproject.codeplex.com/Wikipage

Spud1
+1 for the comment about IDE features. I actually switch off all collapsing in Visual Studio and generally prefer the code-as-plain-text approach. This may be a topic for another question.
Michal Czardybon