views:

570

answers:

7

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?

Thanks

+1  A: 

Both! You normally don't know which one is being used in the IDE : concreate class or its interface. Also, using Resharper (and possibly IntellJ Idea) you can copy the existing comment from the interface to the implementation by context action provided to you.

Hadi Eskandari
I actually think you are wrong here, but I am way too nice to -1 you
willcodejavaforfood
+2  A: 

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.

DeletedAccount
+1  A: 

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
+2  A: 

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
+2  A: 

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
+1  A: 

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
Not automated, requires user action, unfortunately.
DeletedAccount
+14  A: 

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
Cool thanks for the info I didn't know about the @inheritDoc tag
Paul Whelan
Wow... I had no idea {@inheritDoc} existed either! I'll use it regularly from today on.
mcherm