Is there a reason methods in ASP.NET MVC Controllers don't appear to be XML documented?

2.4k Views Asked by At

I'm a huge fan of XML documentation in .NET.

However, I can honestly say I've never seen a tutorial or project where, for example, we had markup like this:

/// <summary>
/// dummy text
/// </summary>
/// <returns>blah</returns>
public ActionResult LogOff() {
    FormsService.SignOut();

    return RedirectToAction("Index", "Home");
}

Instead of:

// **************************************
// URL: /Account/LogOff
// **************************************

public ActionResult LogOff() {
    FormsService.SignOut();

    return RedirectToAction("Index", "Home");
}

Is there any particular reason for this? Am I the only one who wants to document my Controller's methods?

EDIT 1:

And while most controller methods seem to be simple, how about cases detailed in say this question: MVC: How to work with entities with many child entities? ?

2

There are 2 best solutions below

4
On BEST ANSWER

XML documentation is great when the public API needs to be documented for external parties using it. Controllers in my opinion, don't fall into that category.

Also in line with slim controllers, they should be self explanatory as to what they do, especially with attribute metadata such as HttpPost and HttpGet.

Do you envisage a third party using your controllers as an API?

10
On

I usually document my controller actions with a short description of what that action is meant to do, for example:

/// <summary>
/// Controller for viewing and updating the jobs list.
/// </summary>
public class JobsController : Controller
{
    /// <summary>
    /// Displays a list of all jobs for a given site.
    /// </summary>
    /// <param name="siteId">Id of the site to view jobs for.</param>
    public ActionResult Index(string siteId);

    /// <summary>
    /// Displays a detail view of a single job.
    /// </summary>
    /// <param name="id">Id of the job to view.</param> 
    public ActionResult Detail(string id);
}

It's not quite the same as my xml documentation for other classes because these classes will never be used directly, and so its more documentation of the site / page behaviour than anything else. That said I find it useful to have a description of what the action does and what the parameters are, and here is as good a place as any.

Note that I don't include the path - not only because if the path changes the information is out of date, but also becasue it should be obvious by looking at the route mappings what the path will be anyway.

Update / response to comments:

This sort of documentation may seem entirely pointless as the classes are pretty much self describing anyway, however on well named methods in properly structured code this is usually the case with XML documentation anyway. I still believe that this sort of documentation adds value however:

  • It clarifies what the class / method / parameter does in plain english
  • In confirms that nothing special is happening (as opposed to someone just not bothering to write the documentation).

Note that I don't document the return value as there is absolutely nothing useful that I can ever say about it.

You also need to consider that this example is incredibly contrived - it could be that certain parametrs are JSON serialised data, or that a negative value means something completely different. My view on XML documentation is that you should either document nothing, or you should document everything (no matter how obvious). If only half your methods are documented then you can never tell if its because its entirely obvious, or if the developer was simply too lazy - also consider that the purpose of a method may not be as obvious to others as it is to you.

For example I don't bother to write documentation for event handlers (I used to but the comment was always exactly the same).