我写了一段看起来像这样的代码:
propertySupplier.getSomeProperty().ifPresentOrElse(
property -> //do something with the property,
() -> log.error("property not found")
)
然后我把它展示给我的主管。他不喜欢它,并告诉我他宁愿有这样的事情:
Property someProperty = propertySupplier.getSomeProperty();
if(property == null) {
log.error("property not found")
} else {
//do something with the property
}
之所以这样,是因为他想尽快知道某个功能将要做什么(尽早失败)。这是可以理解的,但是我不同意引入null
。因此,我想知道是否存在代码样式指南,以使该功能注释更易于阅读,从而使我可以看到他?为了澄清我不是要征求意见,我知道这里并不常见,而只是为了提供资源来阅读或表明我找不到自己。
答案 0 :(得分:2)
CodeReview论坛的内容。
您的版本更好。但是,我认为以下两者都可以满足:
Property property = propertySupplier.getSomeProperty()
.orElseThrow(() -> {
log.error("property not found");
return new NoSuchElementException();
});