简体   繁体   English

使用双重底片测试条件

[英]Using double negatives to test for conditions

To validate user inputs, I am unsure of which of two alternatives is better 为了验证用户输入,我不确定哪种选择更好

1 1个

isNull( Object input )

2 2

notNull( Object input )

apache's commons lang library chose #2 but I am uncomfortable with double negatives. apache的commons lang库选择了#2,但我对双重否定不满意。 What do you say ? 你说什么 ?

To me, double negatives is always a bad thing. 对我来说,双重否定总是一件坏事。 I'd say stick to isNull. 我会说坚持isNull。 Checking for nullity then makes sense while reading. 然后在阅读时检查是否为空。 The opposite would be 相反的是

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

which reads out "if not is null". 读出“如果不是null”。 Sure it sounds retarded read out loud, but it makes more sense than checking for nullity with option #1. 当然,大声读出声音听起来有些迟钝,但是比使用选项#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". 标准是好事,不应仅仅因为某人“不舒服”就违反标准。

I am uncomfortable with functions which are longer than the code they replace. 对于那些比它们替换的代码更长的函数,我感到不舒服。

I would use x == null or x != null. 我会使用x == null或x!= null。 I agree that double negative isn't clear either. 我同意双重否定也不清楚。

A puzzle for you, when is (s != s) true? 给您一个难题,什么时候(s!= s)正确? A good example of confusing behaviour. 令人困惑的行为的一个很好的例子。 ;) ;)

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. 如果您正在设计自己的API,请以最舒适的方式实现它。 Indeed, there's no strong reason to not to implement both isNull and notNull if that makes you feel happier. 确实,没有充分的理由不同时执行isNullnotNull如果这会使您感到更快乐。

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: 答案取决于许多考虑因素,其中包括:

  • 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. 同时,我的首要建议是选择能提高代码可读性的一切:您想为将来的维护者提供尽可能多的帮助。

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. 如果所讨论的方法只是根据参数是否为null返回布尔null ,那么== null!= null会更好。

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 . 但是,对Apache Commons Lang的引用似乎表明这是有效性检查,并且如果引用为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: FWIW,似乎JDK7将引入notNull方法,如在适当的邮件列表上所讨论的, notNull类似于:

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);
    }
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM