在同一个 if 语句中检查 null 并检查对象属性是一个好习惯吗
在同一个 if 语句中检查 null 并检查对象属性是一个好习惯吗?
考虑以下代码:
if (jamesBlunt != null && jamesBlunt.isReallyBad()) {
// Don't buy his records
}
这个特定的代码是用java编写的,我知道表达式是从左到右计算的,所以从技术上讲它不会抛出NullPointerException,但总的来说,这在任何语言中都是好的做法吗?
Is it good practice to check for null and check object properties in the same if statement?
Consider the following code:
if (jamesBlunt != null && jamesBlunt.isReallyBad()) {
// Don't buy his records
}
This particular code is in java and I know the expression is evaluated from left to right so technically it won't throw a NullPointerException but in general is this good practice in any language?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您使用
OR
语句时,如果jamesBlunt
为 null,则会出现NullPointerException
。您应该使用and
,因为如果左侧语句为 false,则整个表达式将为 false。使用:
当 Java 7 发布时,您可以使用快捷方式,
但在此之前,显式检查
null
将是最佳实践。 (事实上,最好的做法是不使用 jamesBlunt 对象...)As you are using an
OR
statement there will be aNullPointerException
ifjamesBlunt
is null. You should useand
, because if the left statement is false, the whole expression will be false.Use:
When Java 7 is out, you could use the shortcut
But until then the explicit check for
null
would be best practice. (In fact it would be best practice to not use jamesBlunt objects...)我假设
||
是一个拼写错误,而您的意思是&&
:)回答您的问题:这取决于。
jamesBlunt
为 null 是否有意义?如果没有,那么最好的做法是这样:如果
jamesBlunt
为 null 确实有意义,那么你的方法就很好,假设null
和isReallyBad
在语义上意味着同样的事情。如果它们在语义上意味着不同的事物,那么您可能不应该将它们组合在一行上。使用其他语言时您确实需要小心。许多(Java、C++、C# 等)的行为方式相同,但有些可能从右到左求值或延迟求值。请特别注意 Lisp 和 Scheme 等函数式语言,因为它们的行为往往与 Java 和 C# 等面向对象语言不同。
I'll assume the
||
is a typo and you meant&&
:)To answer your question: it depends.
Does it make sense for
jamesBlunt
to ever be null? If not, then it would be better practice to have something like this:If it does make sense for
jamesBlunt
to be null then your approach is fine, assumingnull
andisReallyBad
mean the same thing semantically. If they mean different things semantically then you should probably not be combining them on one line.You do need to be careful in other languages. Many (Java, C++, C# etc) will behave the same way, but some may evaluate from right-to-left or evaluate lazily. Take particular care with functional languages like Lisp and Scheme as they tend to behave differently to object oriented languages like Java and C#.
你想使用 &&而不是“或”。
是的,在同一个 if 语句中检查 null 是一个很好的做法,替代方案(嵌套两个 if)很丑陋,因为会增加更多缩进。
但之前检查也可以:特别是如果你想做一些错误检查。
You want to use && instead of OR.
Yes, it is good practice to check for null in the same if statement, the alternative (nesting two ifs) is ugly because adds more indentation.
But checking before is also OK: specially if you want to do some error checking.
就我个人而言,我会将这些分开。空检查是在函数开始时执行的一种操作,通常用于验证函数参数,通常称为“保护子句”。这些检查通常会引发异常或返回函数定义的错误代码。
这些可以使下面的代码更清晰,避免多层嵌套。如果您希望在相关对象上测试其他内容,它还可以避免重复空检查的需要。
Personally, I would separate these. Null checking is the kind of thing one would do at the beginning of a function and is usually done to verify function parameters and are generally called "guard clauses". These checks will often raise an exception or return an error code as defined by the function.
These can lead to cleaner code below, avoiding multiple levels of nesting. It also prevents the need to repeat the null checks if there are other things you wish to test for on the object in question.