Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good Practices for Method Summary XML Comments

I have been using the method summary XML Comments at the top of my procedures lately and am wondering if there are any logical or good practices related to this.

I never put anything in the remarks because I put the description of the method in the summary tag. What belongs in the summary and what belongs in remarks?

I seldom put anything in the returns tag because it seems like it would be redundant as I usually explain what is being returned in the summary. Should I simply keep the type of object returned in the returns tag?

Can anyone advise on a good, logical, or commonly used approach for these XML comments that would be beneficial to the other programmers on the the team while not requiring the same information being displayed multiple times?

like image 658
NoAlias Avatar asked Feb 09 '11 16:02

NoAlias


People also ask

Should you use XML comments?

XML comments are usefull for generating documentation. If the code is clearly written then you shouldn't need comments to help you understand the code.

What is the advantage of using XML comments?

XML comments help speed development by providing IntelliSense on custom methods and other code constructs. XML comments encourage code reuse by providing a path to generate API style reference documentation from your source code.

How do you write a summary comment in C#?

The first rule for commenting is it should have /// three slash for comments as C# supports C++ style commenting so commenting can be done by // -- two slashes -- but for Documentation /// is necessary. We will go through each one by one. You can add a paragraph to the description by using <para> tag.

How do you write a comment in a method in C#?

Single-line comments start with two forward slashes ( // ). Any text between // and the end of the line is ignored by C# (will not be executed).


1 Answers

The tags that I use the most are:

  • <summary> - purpose of the method / class / member with <see> tag
  • <param name="paramName"> - what is the parameter for
  • <returns> - what does the method return
  • <see cref="referenceToMember"/> - allows to link to refer to another class / member (great for using in constructors)
  • <exception cref="referenceToMember"/> - reference of the exception being thrown by the method
  • <example> <code>... - If you want to give an example of the usage of the method
  • <value> - describes the property value
  • <c> - describes code segments (can be used with <returns>)

Examples

  • <summary> with <see cref=".."/>

    /// <summary>
    /// Initializes a new instance of the <see cref="Form1"/> class.
    /// 
    public Form1()
    {
       InitializeComponent();
    }
  • <returns> with <c>

    /// <summary>
    /// Validates the date.
    /// </summary>
    /// <param name="date">The date.
    /// <returns><c>true</c> if the date is valid; otherwise <c>false</c>.</returns>
    public bool validateDate(string date)
    {
        // Do something
    }

Automatic tag generation

Instead of trying to insert these tags manually, you can also use free visual studio addons like Ghost Doc to generate the required tags.

Use of xml tags

In addition to the providing information in the API (or developer help document), it allows the user of the member to get vital information like the exception types that a method can throw. I quote this example because it can very helpful to know what exception types can be thrown by the helper / model classes. The view / controller class can then catch only those kinds of exceptions and handle them.

like image 103
Devendra D. Chavan Avatar answered Sep 22 '22 08:09

Devendra D. Chavan