简体   繁体   English

哪个是更好的空检查方法?

[英]Which is better way of having a null check?

I came across these two ways of having a null check for a string object. 我遇到了这两种对字符串对象进行空检查的方法。

Given a string object String str = "example"; 给定一个字符串对象String str =“example”;

  1. If(str.someMethod() != null ) or If(str.someMethod() != null )
  2. If (null != str.someMethod())

Why do we prefer the 2nd one ? 为什么我们更喜欢第二个? What is the exact reason behind this, is it related to performance ? 这背后的确切原因是什么,它与性能有关吗?

In your example, it makes absolutely no difference which you do (other than style), because the reason for Yoda checks is to avoid accidentally doing an assignment (but keep reading for why this doesn't matter in Java), and you can't assign to the result of calling a method. 在你的例子中,它与你做的完全没有区别(除了样式),因为Yoda检查的原因是为了避免意外地进行任务(但是继续阅读为什么这在Java中无关紧要),你可以' t分配调用方法的结果。

One of the nice things about Java is that even if you were testing str , eg: 关于Java的一个好处是,即使您正在测试str ,例如:

if (str == null)

vs.

if (null == str)

there would still be no difference, whereas in some of the languages with syntax derived from B (such as C, C++, D, JavaScript, etc.), people do the second (a "Yoda test") to minimize the odds of this bug: 仍然没有区别,而在某些语言中使用B语言(例如C,C ++,D,JavaScript等),人们会做第二次(“尤达测试”)以最小化这种情况BUG:

if (str = null)          // Not an issue in Java

In C or JavaScript, for instance, that would assign null to str , then evaluate the result, coerce it to boolean, and not branch. 例如,在C或JavaScript中,将为str 分配 null ,然后计算结果,将其强制转换为boolean,而不是分支。 But in Java, that's a syntax error the compiler tells you about. 但在Java中,这是编译器告诉你的语法错误。

Java doesn't do that kind of boolean conversion, so the only reason for using Yoda checks in Java is if you're testing booleans, ec Java没有进行那种布尔转换,所以在Java中使用Yoda检查的唯一原因是你是在测试布尔值,ec

boolean flag;
// ...
if (flag == false)

There , you might conceivably do this by accident: 在那里 ,你可能会想到这是偶然的:

if (flag = false)

But since using == and != with booleans is completely unnecessary (you'd just use if (flag) or if (!flag) ), in the real world you don't need Yoda checks with Java at all. 但是因为使用带有布尔值的==!=是完全没必要的(你只需使用if (flag)if (!flag) ),在现实世界中你根本不需要用Java进行Yoda检查。

That doesn't mean people don't still use them, as a matter of their own personal style. 这并不意味着人们仍然不会使用它们,这是他们个人风格的问题。 There's just no objective reason to, in Java. 在Java中,没有客观的理由。

It makes no difference performance-wise, however the Yoda programming pattern have some advantages when it comes to the world of programming skills. 它在性能方面没有任何区别,但是当谈到编程技能的世界时, Yoda编程模式有一些优势。

In your example it would not matter as both cases would throw a NullPointerException (since you're invoking someMethod` of a null instance reference). 在您的示例中,这两个案例都会抛出NullPointerException (因为您正在调用null实例引用的someMethod`)并不重要。

However, say that you wanted to check if str is null. 但是,假设您要检查str是否为null。 In the first case, you'd write if (str == null) and in the second if (null == str) . 在第一种情况下,您将编写if (str == null)并在第二种if (null == str)编写。 Both are the same. 两者都是一样的。 Now say that you have accidently used = instead of == . 现在说你意外地使用=而不是== In Java, it would not matter as the compiler wouldn't let you as the expression doesn't evalute to a boolean value. 在Java中,没关系,因为编译器不会让你因为表达式没有评估布尔值。 But other languages let you do that, more specifically languages that are compiler-free and only use an interperter. 但是其他语言可以让你这样做,更具体地说是没有编译器的语言,只使用一个interperter。 In that case, if you write if (str = null) you'll be assigning null to string and overriding its' current value, which would result in buggy behavior and you chasing after your tail for quite some time. 在这种情况下,如果您编写if (str = null)您将为字符串赋值null并覆盖其当前值,这将导致错误的行为并且您在尾巴上追逐很长一段时间。 However, if you'd write if (null = str) you'll get an error saying you cannot assign a value to null and thus save yourself a lot of time and effort. 但是,如果您编写if (null = str) ,则会收到错误消息,指出您无法将值分配给null ,从而节省了大量的时间和精力。 Again, this is not relevant to JAVA. 同样,这与JAVA无关。

An example which might be relevant for Java, is the use of method invocation on constant values. 可能与Java相关的示例是对常量值使用方法调用。 For example, if (str.equals("constantString") . If str is null you'll get a NullPointerException. However, if you use a Yoad pattern and write if ("constantString".equals(str)) you'll get false as ConstantString does not equal null . This of course is only relevant for comparison, and not say contains etc. 例如, if (str.equals("constantString") 。如果strnull你将得到一个NullPointerException。但是,如果你使用Yoad模式并写if ("constantString".equals(str))你会得到false因为ConstantString不等于null 。这当然只与比较相关,而不是说contains等。

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

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