在同一个 if 语句中检查 null 并检查对象属性是一个好习惯吗

发布于 2024-10-27 01:29:02 字数 270 浏览 0 评论 0原文

在同一个 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 技术交流群。

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

发布评论

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

评论(4

再可℃爱ぅ一点好了 2024-11-03 01:29:02

当您使用 OR 语句时,如果 jamesBlunt 为 null,则会出现 NullPointerException。您应该使用 and,因为如果左侧语句为 false,则整个表达式将为 false。

使用:

if (jamesBlunt != null && jamesBlunt.isReallyBad()) {
   // Don't buy his records
}

当 Java 7 发布时,您可以使用快捷方式,

if(jamesBlunt?.isReallyBad() {
       // Don't buy his records
}

但在此之前,显式检查 null 将是最佳实践。 (事实上​​,最好的做法是使用 jamesBlunt 对象...)

As you are using an OR statement there will be a NullPointerException if jamesBlunt is null. You should use and, because if the left statement is false, the whole expression will be false.

Use:

if (jamesBlunt != null && jamesBlunt.isReallyBad()) {
   // Don't buy his records
}

When Java 7 is out, you could use the shortcut

if(jamesBlunt?.isReallyBad() {
       // Don't buy his records
}

But until then the explicit check for null would be best practice. (In fact it would be best practice to not use jamesBlunt objects...)

古镇旧梦 2024-11-03 01:29:02

我假设 || 是一个拼写错误,而您的意思是 && :)

回答您的问题:这取决于。

jamesBlunt 为 null 是否有意义?如果没有,那么最好的做法是这样:

void buyARecord(Artist jamesBlunt) {
    if (jamesBlunt == null) {
        throw new IllegalArgumentException("James should never be null!");
    }
}

如果 jamesBlunt 为 null 确实有意义,那么你的方法就很好,假设 nullisReallyBad 在语义上意味着同样的事情。如果它们在语义上意味着不同的事物,那么您可能不应该将它们组合在一行上。

使用其他语言时您确实需要小心。许多(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:

void buyARecord(Artist jamesBlunt) {
    if (jamesBlunt == null) {
        throw new IllegalArgumentException("James should never be null!");
    }
}

If it does make sense for jamesBlunt to be null then your approach is fine, assuming null and isReallyBad 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#.

梦忆晨望 2024-11-03 01:29:02

你想使用 &&而不是“或”。

是的,在同一个 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.

埖埖迣鎅 2024-11-03 01:29:02

就我个人而言,我会将这些分开。空检查是在函数开始时执行的一种操作,通常用于验证函数参数,通常称为“保护子句”。这些检查通常会引发异常或返回函数定义的错误代码。

这些可以使下面的代码更清晰,避免多层嵌套。如果您希望在相关对象上测试其他内容,它还可以避免重复空检查的需要。

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.

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