7

这个问题困扰了我很久。在编程过程中,经常会出现一个问题,即对象中是否存在某些东西。出于这个原因,isEmpty发明了这种方法。很好,但实际上我们!isEmpty几乎一直都在使用它。

因此,notEmpty对于像 Java 这样的语言来说,这将是一个更受欢迎的补充。问题是:为什么语言设计者在定义 API 之前不考虑这一点?至少给一个对应的isEmpty

编辑:我的意思是应该有一个notEmpty以及isEmpty. 根据领域的不同,它们都可以使用,但在大多数情况下,当不涉及 UI 时,我认为notEmpty更适用。

EDIT2:要结束讨论,这里有一个例子:

!metadata.isEmpty() == metadata.notEmpty()

我希望我们也有等式的右边。

4

3 回答 3

6

isEmpty follows the naming convention of other syntactically positive isXXX methods such as isVisible and isEnabled

于 2013-08-16T10:46:10.793 回答
4

I think it follows the convention of using is[Something]() for booleans in place of get[Something]().

Also, having to use !notEmpty() might get a bit confusing!

于 2013-08-16T10:46:07.340 回答
2

我的观点是,它更合乎逻辑,因为 like==!=. 逻辑上最好有isEmpty!isEmpty,很容易理解。

于 2013-08-16T10:47:57.897 回答