java - 我应该在调用方法时发表评论吗?

标签 java javadoc comments

<分区>

我想知道我是否应该在调用方法之前发表评论。例如:

//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/

相关文章:

protected 类的 Javadoc 方法

c++ - 在带有语句的行之前通过单行注释进行错误处理

syntax - 注释掉一部分Vue模板元素

java - Scala 是否对小型集合使用特殊实现?

java - Maven 错误 : -fuzzyMatcher TYPEVAR

java - 将 JNA 指针从一个 Java 应用程序发送到另一个 Java 应用程序

java - 我的代码的每个部分真的都需要 JavaDoc/Comments 吗?

php - 使用 WP_Comment_Query 获取评论

java - 使用Java进行RSA解密

java - GCM SERVICE_NOT_AVAILABLE 错误