<分区>
我想知道我是否应该在调用方法之前发表评论。例如:
//calling method
MethodCall();
或者 javadoc 对方法头的注释是否足够好,例如:
/**
some method
*/
public static void() {
Statements;
}
我应该使用哪个还是应该同时使用两者?
<分区>
我想知道我是否应该在调用方法之前发表评论。例如:
//calling method
MethodCall();
或者 javadoc 对方法头的注释是否足够好,例如:
/**
some method
*/
public static void() {
Statements;
}
我应该使用哪个还是应该同时使用两者?
最佳答案
我在生产代码中经常看到这种情况,很多时候我发现自己想知道为什么会有一些注释。记住好的代码注释本身。
例子
public void doSomething() {
// Some code
}
public static void main(String[] args)
{
// Calling doSomething()
doSomething();
}
从代码中可以清楚地看出,您正在调用 doSomething
。现在,如果在方法名称中不清楚,该方法的作用(或为什么它是相关的)那么一定要评论它:
// Calling doSomething() to establish a connection to the Database.
doSomething();
但是你必须问问自己,什么更有意义?
而且肯定是后者。
public void establishDatabaseConnection() {
// Some code
}
更有意义。
总结
对我来说,评论指南很简单:
If it isn't explicitly clear, why you're calling a method in a given context, then first check the name of that method. If that name could be changed to add clarity, then change it. If the name is as clear as possible, and your code is simply complex, then you can add a comment.
关于java - 我应该在调用方法时发表评论吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21271211/