本文介绍了Guava 的 Optional 类有什么意义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近读到了这个,看到有人使用这个类,但在几乎所有的情况下,使用 null 也可以 - 如果不是更直观的话.有人可以提供一个具体的例子,其中 Optional 可以实现 null 不能或以更简洁的方式实现的东西吗?我唯一能想到的就是将它与不接受 null 键的 Maps 一起使用,但即使这样也可以通过侧面映射"来完成.null 的值.谁能给我一个更有说服力的论据?

I've recently read about this and seen people using this class, but in pretty much all cases, using null would've worked as well - if not more intuitively. Can someone provide a concrete example where Optional would achieve something that null couldn't or in a much cleaner way? The only thing I can think of is to use it with Maps that don't accept null keys, but even that could be done with a side "mapping" of null's value. Can anyone provide me with a more convincing argument?

推荐答案

Guava 团队成员在这里.

Guava team member here.

null 的最大缺点可能是它在任何给定上下文中的含义并不明显:它没有说明性的名称.null 并不总是很明显的意思是这个参数没有值"——哎呀,作为返回值,有时它意味着错误",甚至成功"(!!),或者只是正确答案是什么".Optional 通常是您将变量设为可空时实际指的概念,但并非总是如此.如果不是,我们建议您编写自己的类,类似于 Optional 但使用不同的命名方案,以明确您的实际含义.

Probably the single biggest disadvantage of null is that it's not obvious what it should mean in any given context: it doesn't have an illustrative name. It's not always obvious that null means "no value for this parameter" -- heck, as a return value, sometimes it means "error", or even "success" (!!), or simply "the correct answer is nothing". Optional is frequently the concept you actually mean when you make a variable nullable, but not always. When it isn't, we recommend that you write your own class, similar to Optional but with a different naming scheme, to make clear what you actually mean.

但我想说 Optional 的最大优势不在于可读性:优势在于其防白痴性.如果您希望程序完全编译,它会迫使您主动考虑不存在的情况,因为您必须主动打开 Optional 并解决该情况.Null 使得简单地忘记事情变得非常容易,虽然 FindBugs 有帮助,但我认为它几乎不能解决这个问题.当您返回可能存在"也可能不存在"的值时,这一点尤其重要.您(和其他人)更可能忘记 other.method(a, b) 可能返回 null 值,而不是您可能忘记 当你实现 other.method 时,a 可以是 null.返回 Optional 使调用者不可能忘记这种情况,因为他们必须自己解开对象.

But I would say the biggest advantage of Optional isn't in readability: the advantage is its idiot-proof-ness. It forces you to actively think about the absent case if you want your program to compile at all, since you have to actively unwrap the Optional and address that case. Null makes it disturbingly easy to simply forget things, and though FindBugs helps, I don't think it addresses the issue nearly as well. This is especially relevant when you're returning values that may or may not be "present." You (and others) are far more likely to forget that other.method(a, b) could return a null value than you're likely to forget that a could be null when you're implementing other.method. Returning Optional makes it impossible for callers to forget that case, since they have to unwrap the object themselves.

出于这些原因,我们建议您使用 Optional 作为方法的返回类型,但不必在方法参数中使用.

For these reasons, we recommend that you use Optional as a return type for your methods, but not necessarily in your method arguments.

(顺便说一下,这完全来自此处的讨论.)

(This is totally cribbed, by the way, from the discussion here.)

这篇关于Guava 的 Optional 类有什么意义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 15:38