Comment the interface, implementation or both?

后端 未结 9 1271
萌比男神i
萌比男神i 2020-12-13 03:08

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

/// 
/// Foo Interface
/// 
public interface Fo         


        
相关标签:
9条回答
  • 2020-12-13 03:39

    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

    0 讨论(0)
  • 2020-12-13 03:40

    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.

    0 讨论(0)
  • 2020-12-13 03:41

    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.

    0 讨论(0)
  • 2020-12-13 03:51

    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:

    • Official javadoc documentation
    • Some unofficial advice.
    0 讨论(0)
  • 2020-12-13 03:53

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

    0 讨论(0)
  • 2020-12-13 03:53

    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.

    0 讨论(0)
提交回复
热议问题