158

I imagine that we all (when we can be bothered!) comment our interfaces. e.g.

/// <summary>
/// Foo Interface
/// </summary>
public interface Foo
{
    /// <summary>
    /// Will 'bar'
    /// </summary>
    /// <param name="wibble">Wibble factor</param>
    void Bar(string wibble);
}

Do you also comment the implementation (which may also be provided to clients, e.g. as part of a a library)? If so how do you manage keeping the two in sync? Or do you just add a 'See interface for documentation' comment?

Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
ng5000
  • 12,330
  • 10
  • 51
  • 64
  • A duplicate snuck through here: https://stackoverflow.com/questions/1875440/code-commenting-do-you-put-your-code-comments-on-interfaces-or-on-concrete-clas – bytedev Aug 17 '19 at 14:30

9 Answers9

120

As a general rule, I use the same DRY (Don't Repeat Yourself) principle as with code:

  • on interface, document the interface
  • on implementation, document the implementation specifics

Java specific: when documenting the implementation, use {@inheritDoc} tag to "include" javadocs from the interface.

For more information:

Neeme Praks
  • 8,956
  • 5
  • 47
  • 47
  • Cool thanks for the info I didn't know about the @inheritDoc tag – Paul Whelan Apr 17 '09 at 10:17
  • Wow... I had no idea {@inheritDoc} existed either! I'll use it regularly from today on. – mcherm Jul 02 '10 at 17:07
  • 50
    For C#, you can use ``, which is supported by SandCastle. ([More info...](http://www.ewoodruff.us/shfbdocs/html/79897974-ffc9-4b84-91a5-e50c66a0221d.htm)) – Daniel A.A. Pelsmaeker Jul 08 '12 at 19:51
  • 2
    Properties and other elements within an inherited class does not show the XML documentation in the tooltip when only specified on the interface. For external use of the same class, it is visible. This might be a bug with Visual Studio 2015. – SondreB Aug 18 '15 at 21:33
  • You don't have to use @inheritDoc in Eclipse because the IDE will automatically pull comments from interface even if you use a variable of the type of implementation class (which is a wrong practice as you should always use interface for the variable type!) – Marek Halmo Feb 07 '17 at 12:34
  • 2
    Here's an updated version of the link @Virtlink provided for the Sandcastle/SHFB `inheritdoc` page: http://ewsoftware.github.io/XMLCommentsGuide/html/86453FFB-B978-4A2A-9EB5-70E118CA8073.htm – weir Mar 19 '18 at 13:36
  • 2
    seems to work with Visual Studio 2019 in C#. If you use it the intellisense will display the comment from the interface. – Mog0 Apr 17 '20 at 16:17
31

C# usage:

Interface can look like this:

    /// <summary>
    /// Helper class to access various properties for the current site.
    /// </summary>
    public interface ISiteHelper
    {
        /// <summary>
        /// Gets the site id of the current site
        /// </summary>
        /// <returns>The site id.</returns>
        int GetSiteID();
    }
}

Implementation can look like this:

/// <inheritdoc />
public class SiteHelper: ISiteHelper
{
    public int GetSiteID()
    {
        return CommonRepository.GetSiteID();
    }
}

Official reference for <inheritdoc />: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/xmldoc/recommended-tags#inheritdoc

Dinei
  • 4,494
  • 4
  • 36
  • 60
Raghav
  • 8,772
  • 6
  • 82
  • 106
  • 4
    This only works for methods. `InheritDoc` on the class will show the documentation for `Object`. – Wouter Nov 18 '21 at 11:33
10

The interface only. Commenting both is duplication and it's likely that the two sets of comments will eventually get out of sync if the code changes. Comment the implementation with "implements MyInterface"... Things like Doxygen will generate docs that include the derived docs into the docs for the implementation anyway (if you set them up correctly).

Len Holgate
  • 21,282
  • 4
  • 45
  • 92
7

If you use the GhostDoc addin, it updates the implementation with the comment from the interface when you right click and select "Document This" on the method.

NikolaiDante
  • 18,469
  • 14
  • 77
  • 117
6

For C# it depends IMO: If you use explicit interface implementations, then I wouldn't document the implementation.

However if you implement the interface directly and expose the members of the interface with your object then these methods must be documented too.

As Nath said, you can use GhostDoc to automatically insert the documentation of an interface into the implementation. I mapped the Document This command to the Ctrl+Shift+D shortcut and its one of the keystrokes I almost automatically press. I believe ReSharper also has the option to insert the documentation of the interface, when it implements the methods for you.

grover
  • 2,265
  • 1
  • 16
  • 15
4

Commenting the interface should be enough documentation to figure out how to use the actual implementation. The only time that I would add comments to the implementation is if it has private functions that were inserted to satisfy the interface, however they would be internal only comments and would not be seen in documentation online or available to clients.

Implementations are just that, as long as they conform to the interface there is no need to document them separately.

X-Istence
  • 16,324
  • 6
  • 57
  • 74
4

We just comment the interface, comments are so easy to get out of sync with either the derived or base class/interface that's it's nice to have it in just one place.

Although it looks like @Nath maybe suggesting an automated documentation tool that helps keep things together (sounds cool if you use that). Here at WhereIWorkAndYouDontCare the comments are for dev so a single place in the code is preferred

Jiminy
  • 605
  • 10
  • 17
2

You can certainly comment both but then you have the problem of maintaining both (as previously mentioned). However, in this day and age is any consuming code really not going to be using IoC/DI and not use the interface? Given this if you only want to bother commenting one I would strongly suggest commenting the interface. This way the consumer of your code will more than likely get the nice intellisense hints.

bytedev
  • 8,252
  • 4
  • 48
  • 56
1

I created a tool that post-processes the XML documentation files to add support for the <inheritdoc/> tag.

While it doesn't help with Intellisense in source code, it does allow the modified XML documentation files to be included in a NuGet package and therefore works with Intellisense in referenced NuGet packages.

It's at www.inheritdoc.io (free version available).

K Johnson
  • 478
  • 5
  • 14
  • Note that is supported by Sandcastle Help File Builder too, and is documented here: http://ewsoftware.github.io/XMLCommentsGuide/html/86453FFB-B978-4A2A-9EB5-70E118CA8073.htm. Just spotted that this was also mentioned above. – Olly Jan 18 '18 at 17:05