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 07:46

Ideally, only the interface needs to be documented, since it defines the contract that every concrete implementation needs to fulfill.

查看更多
乱世女痞
3楼-- · 2020-03-12 07:49

Only for interfaces. Because in this case I don't need to synchronize them. My IDE helps me to see interface comments in concrete classes. And api document generator does the same.

查看更多
我欲成王,谁敢阻挡
4楼-- · 2020-03-12 07:50

Both, but I wish there was built in functionality to keep them in sync

查看更多
Lonely孤独者°
5楼-- · 2020-03-12 07:51

A tag <referTo>System. .... </referTo> to link the comments would be ideal

查看更多
家丑人穷心不美
6楼-- · 2020-03-12 07:53

Your example code doesn't use explicit interface implementation. The client of your code is going to need both since s/he can invoke the method either through a class object or interface reference. With explicit interface implementation you can omit the class method comment since the client can never see it. This is assuming you are using XML documentation to generate IntelliSense info.

查看更多
爷、活的狠高调
7楼-- · 2020-03-12 08:01

I don't really use them at all. Instead I make sure to structure the code and name all methods and variables in a way that its obvious what they do without comments. The problem with comments is that they don't compile and don't execute and are not tested by your unit tests, so its pretty much impossible to keep them in synch with the code.

查看更多
登录 后发表回答