【发布时间】:2010-09-08 20:43:08
【问题描述】:
您是否经常在 API 文档(例如“公共函数的 javadoc”)中看到“值限制”的描述以及经典文档?
注意:我不是在说comments within the code
“价值限制”是指:
- 参数是否可以支持空值(或空字符串,或...)?
- “返回值”是否可以为 null 或保证永远不会为 null(或可以为“空”,或者...)?
示例:
我经常看到的(无法访问源代码)是:
/**
* Get all readers name for this current Report. <br />
* <b>Warning</b>The Report must have been published first.
* @param aReaderNameRegexp filter in order to return only reader matching the regexp
* @return array of reader names
*/
String[] getReaderNames(final String aReaderNameRegexp);
我希望看到的会是:
/**
* Get all readers name for this current Report. <br />
* <b>Warning</b>The Report must have been published first.
* @param aReaderNameRegexp filter in order to return only reader matching the regexp
* (can be null or empty)
* @return array of reader names
* (null if Report has not yet been published,
* empty array if no reader match criteria,
* reader names array matching regexp, or all readers if regexp is null or empty)
*/
String[] getReaderNames(final String aReaderNameRegexp);
我的意思是:
当我使用一个包含 getReaderNames() 函数的库时,我通常甚至不需要阅读 API 文档来猜测它的作用。但我需要确定如何使用它。
当我想使用这个函数时,我唯一关心的是:在参数和返回值方面我应该期待什么?这就是我安全设置参数和安全测试返回值所需要知道的全部内容,但我几乎从未在 API 文档中看到此类信息...
编辑:
这会影响 checked or unchecked exceptions 的使用与否。
你怎么看?值限制和API,它们是否属于一起?
【问题讨论】:
标签: language-agnostic documentation comments design-by-contract