views:

264

answers:

9

I'm going through some new code I just wrote and adding NDoc sytle comments to my classes and methods. I'm hoping to generate a pretty good MSDN style document for reference.

In general, what are some good guidelines when writing comments for a class and for a method? What should the NDoc comments say? What should they not say?

I find myself looking at what the .NET framework comments say, but that gets old fast; if I could have some good rules to guide myself, I could finish my docs a lot faster.

+1  A: 

If you end up with comments that don't add any value, they're just wasteful.

For example

/// <summary>
/// Get's 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.

To often code is littered with these superfluous comments.

Chad
Yes, I understand comments can provide no value. That's why I'm looking for guidelines on comments that DO provide value.
Esteban Araya
You asked for what they should, and should not say. Since many comments ARE the garbage I outlined, this was a valid answer, and your comment is "jerk-ish".
Chad
I think that is more an example of poor documentation, not an example of where it should not be used. Public methods should likely have additional documentation, such as expected exceptions, preconditions, etc. For example, what does that method do when action is null?
joseph.ferris
Although I agree with your on the topic of superfulous comments, superfulous documentation is a different matter. In some cases it may be that you simply have nothing extra to document and your xml doc string is just an echo of the method name, however I still add that docsctring - partly because it confirms that the method is as simple as it seems (rather than someone not bothering to document the method), but mostly because it just looks **wrong** if its missing.
Kragen
+3  A: 

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 ..."

Matt Greer
@Matt: Great! Any ideas for methods and classes?
Esteban Araya
In all honesty the only company that takes doc comments seriously is Microsoft :) I would just browse their comments and get a feel for how they do them. They definitely have standards on how comments should be formatted and what they should say. MS also does a good job of indicating what exceptions a method throws. Sadly, doc comments end up being used as a band aid to fix minor issues in the C# language IMO (like relying on a comment or compiler error to find out if a property is read only, that irks me)
Matt Greer
@Matt, I take doc comments seriously and I don't work for Microsoft. Between good documentation comments, GhostDoc and Sandcastle/Sandcastle Help File Builder, our core libraries have a website developers can go to for reference. I **really** dislike having to intuit usage of methods/properties by stepping through code rather than reading concise doc.
Jesse C. Slicer
A: 

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.

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.

luvieere
@luvieere: I undestand the benefits and the how of XML documentation. I need a little bit of help on what people actually find useful in these comments.
Esteban Araya
@Esteban Araya as I've said, proximity to the actual code you're documenting, allowing you to document changes on the spot, without needing to switch to another app and search for the appropriate place to modify.
luvieere
A: 

One thing about comments is UPDATING them. Too many people alter a function then don't change the comment to reflect the change.

Brian
True. We have remedied this in the past by making it a part of a standard code review checklist.
joseph.ferris
+10  A: 

In comments used to build API documentation, you should:

  • 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.

Jeff Sternal
+1. I really like the pre-condtions tip.
Esteban Araya
+1. I think the predominant focus on documentation should be for public interfaces, even more when generating external documentations (doxygen, NDoc, etc). Clients don't need to know every nook and cranny of your class. The implementation details don't need to be documented in this format; the predominant focus should be on the public interface, how it is used, the pre/post conditions, and other things Jeff pointed out.
+3  A: 

StyleCop provides hints for code and commenting style. The suggestions it gives are in line with the MSDN documentation style.

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.

Niels van der Rest
I rather find StyleCop a handy reminding tool for when I've added/removed a parameter from a method and spaced on updating the `<params>` nodes. From what I remember from my last gig, R# did that too real-time.
Jesse C. Slicer
Another option: Resharper does that in the UI.
F.Aquino
+1  A: 

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).

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. 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.

mikemanne
+1  A: 

Don't forget what's a valid XML is. For example:

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

(Error: invalid XML).

Pavel Radzivilovsky
A: 

I've written a blog post about it: http://blog.gauffin.org/2010/06/my-name-is-more-docu-more/

jgauffin