Java 8中可选类型的有效用法 [英] Valid usage of Optional type in Java 8
问题描述
class Foo {
private)这是Java 8中可选类型的有效(预期)用法吗?最终整数ID;
private final String name;
private final String countryCode;
Foo(final Integer id,final String name,final String countryCode){
this.id = id;
this.name = name;
这个。 countryCode = countryCode;
}
public String getName(){
return name;
}
public String getCountryCode(){
return countryCode;
}
}
class FooSrv {
private可选< Foo> getFoo(final Integer id){
return id!= null&& id == 1? Optional.of(new Foo(id,Bar,US)):Optional.empty();
}
private boolean isFooValid(final Integer id){
return getFoo(id)
.filter(f - >Bar.equals(f。 getName())&&US.equals(f.getCountryCode()))
.map(f - > true)
.orElse(false);
}
}
I我们再来看看这个。
这是一个有效的用法吗?是的,从狭义上讲,它编译并产生你期望的结果。
这是用途吗?不。现在,有时候事情会发现超出它们原来的用处,如果这样做成功,那就太好了。但对于可选
,我们发现通常情况下效果不好。
布赖恩戈茨,我在我们的JavaOne 2015中讨论了 Optional
的一些问题使用Java 8 Lambdas和Streams进行API设计:
主要使用可选
如下所示:(幻灯片36)
可选用于为明确需要表示无结果和使用
null $ c的情况,为库方法返回类型提供有限 $ c>因为这绝对会导致错误。
从可选的
链接方法的能力无疑是非常酷的,在某些情况下它可以减少条件逻辑的混乱。但通常情况下这是行不通的。一种典型的代码异味是代替使用方法链接到 handle 从某个方法返回的 Optional
,它创建 >一个可选的
从可为空的东西,为了链方法和避免条件。下面是一个在行动中的例子(同样来自我们的演示,幻灯片42):
// BAD
字符串过程(String s){
return Optional.ofNullable(s).orElseGet(this :: getDefault);
}
// GOOD
String process(String s){
return(s!= null)? s:getDefault();
$ / code>
使用可选的方法
更长,而且大多数人发现它比传统代码更晦涩难懂。不仅如此,它还会产生额外的垃圾,因为没有任何理由。
底线:仅仅因为您可以做某件事并不意味着您应该完成。
Is this a valid (intended) usage of Optional type in Java 8?
class Foo {
private final Integer id;
private final String name;
private final String countryCode;
Foo(final Integer id, final String name, final String countryCode) {
this.id = id;
this.name = name;
this. countryCode = countryCode;
}
public String getName() {
return name;
}
public String getCountryCode() {
return countryCode;
}
}
class FooSrv {
private Optional<Foo> getFoo(final Integer id) {
return id != null && id == 1 ? Optional.of(new Foo(id, "Bar", "US")) : Optional.empty();
}
private boolean isFooValid(final Integer id) {
return getFoo(id)
.filter(f -> "Bar".equals(f.getName()) && "US".equals(f.getCountryCode()))
.map(f -> true)
.orElse(false);
}
}
I'll take another swing at this.
Is this a valid usage? Yes, in the narrow sense that it compiles and produces the results that you're expecting.
Is this intended usage? No. Now, sometimes things find usefulness beyond what they were originally for, and if this works out, great. But for Optional
, we have found that usually things don't work out very well.
Brian Goetz and I discussed some of the issues with Optional
in our JavaOne 2015 talk, API Design With Java 8 Lambdas and Streams:
The primary use of Optional
is as follows: (slide 36)
Optional is intended to provide a limited mechanism for library method return types where there is a clear need to represent "no result," and where using
null
for that is overwhelmingly likely to cause errors.
The ability to chain methods from an Optional
is undoubtedly very cool, and in some cases it reduces the clutter from conditional logic. But quite often this doesn't work out. A typical code smell is, instead of the code using method chaining to handle an Optional
returned from some method, it creates an Optional
from something that's nullable, in order to chain methods and avoid conditionals. Here's an example of that in action (also from our presentation, slide 42):
// BAD
String process(String s) {
return Optional.ofNullable(s).orElseGet(this::getDefault);
}
// GOOD
String process(String s) {
return (s != null) ? s : getDefault();
}
The method that uses Optional
is longer, and most people find it more obscure than the conventional code. Not only that, it creates extra garbage for no good reason.
Bottom line: just because you can do something doesn't mean that you should do it.
这篇关于Java 8中可选类型的有效用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!