Code Commenting: Do you put your code comments on

2020-03-12 07:12发布

What is the best practice in documenting classes and interfaces. Say if you have a concrete class called Foo, that derives from an interface called IFoo. Where do you put your comments for your methods? Do you duplicate your comments on the Interface as well as the concrete class?

Here is an example where comments are duplicated:

public class Foo : IFoo
{
    /// <summary>
    /// This function does something
    /// </summary>        
    public void DoSomething()
    {
    }
}

public interface IFoo
{
    /// <summary>
    /// This function does something
    /// </summary>        
    void DoSomething();
}

9条回答
祖国的老花朵
2楼-- · 2020-03-12 08:11

I would put comments on both.

On interfaces I would comment on the intent behind the interface members and usage.

On implementations I would comment on the reasons for the specific implementation.

查看更多
smile是对你的礼貌
3楼-- · 2020-03-12 08:13

I generally put them on both, however, they do not say the same thing. The interface's comment should describe the abstract purpose of this method/interface. While the concrete comment will talk about the implementation specifics of the method/class in the context of the interface's purpose.

查看更多
够拽才男人
4楼-- · 2020-03-12 08:13

I put them in both, but its a pain keeping them in sync, when in doubt I only put them on the interface.

I do this because I like the tooltip when using the code, which should almost always be using the interface...

查看更多
登录 后发表回答