使用双重否定来测试条件

发布于 2024-08-07 15:47:09 字数 214 浏览 1 评论 0原文

为了验证用户输入,我不确定两个替代方案中哪一个更好

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

乖乖公主 2024-08-14 15:47:09

对我来说,双重否定总是一件坏事。
我想说坚持 isNull 。在阅读时检查无效性就有意义了。
相反的情况是

if (! isNull(o) )
    // ...

读出“if not is null”。当然,这听起来有点迟钝,但它比使用选项 #1 检查无效性更有意义。

if (! isNotNull(o))
    // ...

一个让你倒带并说“嘿,等一下......如果不是的话就不是......”的陈述。


但是如果已经有一个标准在方法名称中包含否定,请遵守它。标准是好东西,不应该因为某人“不舒服”而被打破。

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

if (! isNull(o) )
    // ...

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.

if (! isNotNull(o))
    // ...

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".

小耗子 2024-08-14 15:47:09

我对比它们替换的代码长的函数感到不舒服。

我会使用 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. ;)

野生奥特曼 2024-08-14 15:47:09

老实说,我认为这没有多大区别。

如果您正在设计自己的 API,请以您最熟悉的方式实现它。事实上,如果您感觉更快乐,就没有充分的理由不同时实现 isNullnotNull

但我认为放弃单独使用某些第三方库的计划是愚蠢的,因为你对双重否定“感到不舒服”。他们确实没有那么令人困惑。

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 and notNull 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.

○愚か者の日 2024-08-14 15:47:09

这里的答案取决于很多考虑因素,其中一些是:

  • 您的编程环境最自然地支持什么?
  • 同一项目中其他类似代码的作用是什么?
  • 您正在遵循什么编码标准(如果有)?
  • 对于阅读您的代码的其他人来说,哪种方法更容易理解?

如果您想要一个更经过深思熟虑的答案,请让我们了解更多详细信息。与此同时,我的首要建议是尽一切努力增强代码的可读性:您希望为未来的维护人员提供尽可能多的帮助。

The answer here depends on so many considerations, of which some are:

  • What does your programming environment support most naturally?
  • What does other similar code in the same project do?
  • What coding standards are you working to, if any?
  • Which approach is easier to understand for others reading your code?

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.

够钟 2024-08-14 15:47:09

我对这个问题有点困惑。如果相关方法只是根据参数是否为 null 返回一个布尔值,那么 == null!= null 会更好。

然而,对 Apache Commons Lang 的引用似乎表明这是有效性检查,如果引用为 null,则应抛出适当的异常。在这种情况下,您无法轻松反转输出。

FWIW,看起来 JDK7 将引入一个 notNull 方法,正如在适当的邮件列表上讨论的那样,大致如下:

public static <T> T notNull(T obj)

用作:

import static java.util.Object.notNull;

public final MyClass {
    private final Thing thing;
    public MyClass(Thing thing) {
        this.thing = notNull(thing);
    }
    ...

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:

public static <T> T notNull(T obj)

Used as:

import static java.util.Object.notNull;

public final MyClass {
    private final Thing thing;
    public MyClass(Thing thing) {
        this.thing = notNull(thing);
    }
    ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文