使用双重否定来测试条件
为了验证用户输入,我不确定两个替代方案中哪一个更好
1
isNull( Object input )
2
notNull( Object input )
apache 的 commons lang 库选择了#2,但我对双重否定感到不舒服。你怎么说 ?
To validate user inputs, I am unsure of which of two alternatives is better
1
isNull( Object input )
2
notNull( Object input )
apache's commons lang library chose #2 but I am uncomfortable with double negatives. What do you say ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对我来说,双重否定总是一件坏事。
我想说坚持 isNull 。在阅读时检查无效性就有意义了。
相反的情况是
读出“if not is null”。当然,这听起来有点迟钝,但它比使用选项 #1 检查无效性更有意义。
一个让你倒带并说“嘿,等一下......如果不是的话就不是......”的陈述。
但是如果已经有一个标准在方法名称中包含否定,请遵守它。标准是好东西,不应该因为某人“不舒服”而被打破。
To me, double negatives is always a bad thing.
I'd say stick to isNull. Checking for nullity then makes sense while reading.
The opposite would be
which reads out "if not is null". Sure it sounds retarded read out loud, but it makes more sense than checking for nullity with option #1.
A statement which makes you rewind and say "hey, wait a minute... if not is not...".
But if there is a standard already with having negatives in method names, stick to it. Standards are good things, and should not be broken just because someone is "uncomfortable".
我对比它们替换的代码长的函数感到不舒服。
我会使用 x == null 或 x != null。我同意双重否定也不清楚。
对您而言,这是一个难题:(s != s) 什么时候为真?令人困惑的行为的一个很好的例子。 ;)
I am uncomfortable with functions which are longer than the code they replace.
I would use x == null or x != null. I agree that double negative isn't clear either.
A puzzle for you, when is (s != s) true? A good example of confusing behaviour. ;)
老实说,我认为这没有多大区别。
如果您正在设计自己的 API,请以您最熟悉的方式实现它。事实上,如果您感觉更快乐,就没有充分的理由不同时实现
isNull
和notNull
。但我认为放弃单独使用某些第三方库的计划是愚蠢的,因为你对双重否定“感到不舒服”。他们确实没有那么令人困惑。
Honestly, I don't think it makes much difference.
If you are designing your own API, implement it the way you are most comfortable with. Indeed, there's no strong reason to not to implement both
isNull
andnotNull
if that makes you feel happier.But I think it would be silly to abandon plans to use some third-party library solely because you are "uncomfortable with" double negatives. They really aren't that confusing.
这里的答案取决于很多考虑因素,其中一些是:
如果您想要一个更经过深思熟虑的答案,请让我们了解更多详细信息。与此同时,我的首要建议是尽一切努力增强代码的可读性:您希望为未来的维护人员提供尽可能多的帮助。
The answer here depends on so many considerations, of which some are:
If you want a more considered answer, do let us know some more of the details. In the meantime, my top tip would be to go for whatever enhances readability of the code: you want to give future maintainers as much help as you can.
我对这个问题有点困惑。如果相关方法只是根据参数是否为
null
返回一个布尔值,那么== null
或!= null
会更好。然而,对 Apache Commons Lang 的引用似乎表明这是有效性检查,如果引用为
null
,则应抛出适当的异常。在这种情况下,您无法轻松反转输出。FWIW,看起来 JDK7 将引入一个
notNull
方法,正如在适当的邮件列表上讨论的那样,大致如下:用作:
I am a little confused by the question. If the methods in question are just returning a boolean depending upon whether the argument is
null
, then== null
or!= null
are much better.However, the reference to Apache Commons Lang seem to indicate that this is validity checking, and an appropriate exception should be thrown is the reference is
null
. In that case you can't invert the output easily.FWIW, it looks as if JDK7 will introduce a
notNull
method, as discussed on an appropriate mailing list, something along the lines of:Used as: