执行javadoc注释

deg*_*ath 5 java javadoc

我正在阅读很多关于javadoc的文章,但是当"样板"开始时仍然无法管理.在这个例子中:

/**
 * Returns a list of tasks for specific user
 * @param userId
 * @return Selected list of tasks
 */
List<Task> getTasksForUser(Integer userId);

/**
 * Returns a list of tasks in chosen month and year
 * @param month
 * @param year
 * @return selected list of tasks
 */
List<Task> getTasks(Integer month, Integer year);
Run Code Online (Sandbox Code Playgroud)

我可以以某种方式执行它们以减少样板或我应该删除它们吗?

为什么75%的文章称为"Javadoc的最佳实践,我们有重复?"例如:

/**
 * Returns a list of tasks using params month, year
 * @param month
 * @param year
 * @return a list of tasks
 */
List<Task> getTasks(Integer month, Integer year);
Run Code Online (Sandbox Code Playgroud)

是不是写了两次相同的东西?

Gho*_*ica 8

在某种程度上,这是关于"风格".尽管如此,让我们来看看:

/**
 * Returns a list of tasks for specific user
 * @param userId
 * @return Selected list of tasks
 */
List<Task> getTasksForUser(Integer userId);
Run Code Online (Sandbox Code Playgroud)

有些人认为有一定的优点

  • 一个人类可读的描述,告诉你方法的作用
  • 使用@param/@return标签的其他信息

你可以例如重写这个:

/**
 * Returns a list of tasks for specific user.
 * @param userId denotes the user by his numeric id
 * @return Selected list of tasks (maybe empty, never null)
 */
List<Task> getTasksForUser(Integer userId);
Run Code Online (Sandbox Code Playgroud)

所以 - 在你的例子中,有空间使用额外的标签来提供实际上不同的信息:我的版本中的每一行都有一定的用途,而你的例子只是重复相同的信息,尽管使用略有不同的措辞.

但正如所说,最后,这是一个风格问题,关键在于:你应该选择你(和你的同伴)发现对你的环境最有帮助的"风格".

请注意:不是一遍又一遍地重复"明显"的事情,更有用的评论可能看起来像这样:

/**
 * @return The tasks selected for the given user (maybe empty, never null)
 * @throws UnknownUserException when <code>userId></code> is not known
 */
List<Task> getTasksForUser(Integer userId);
Run Code Online (Sandbox Code Playgroud)

这基本上是"我的"首选风格 - 与单一的@return线一起使用.但是提到关键方面,比如 - 如果......这个方法会抛出运行时异常

最后要注意的是:拥有"空"@param行(仅提供参数名称)是明确禁止的.这些线条告诉你什么 - 但你仍然需要花时间阅读并忽略它们.这样的事情是浪费.避免这样做.