且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Optional.of()方法进行方法链接是个好主意吗?

更新时间:2022-03-21 22:38:04

如果你不介意没有代码的答案,只需要0.02美元。

If you don’t mind an answer without code, just my 0.02$.

我们在我的工作场所(代码审查)一直在争论这个问题。坦率地说,我有时会丢失它是否正确(由它的创造者阅读)或不是。我甚至不认为在创作时,Stuart Marks和其他人都确定如何(ab)使用它。与Java语言中的任何其他功能一样,它会被滥用,确保这一点。但是随着时间的推移,会出现***实践,人们会使用它。

We have been fighting over this at my work place (in code reviews) quite a lot. To be frank I am sometimes lost as to when it is correct (read intended by the creators of it) or not. I don’t even think that at the time of creation, Stuart Marks and the others were sure how this would be (ab)used. Like any other feature in the Java language, it will get abused, be sure of that. But over time, best practices will appear and people will use that.

我倾向于站在 iff 一边不会影响性能,你的用法还可以。至少它太容易阅读(在这里为自己说话),与if else检查相比;但同样可能是我(也)做了很多Java-8及以上的代码。

I tend to be on the side that iff it does not hurt performance, your usage is OK. At least its sooo easy to read (speaking for myself here), compared to if else checks; but again it might be me doing (too) much Java-8 and above code.

话虽这么说,把它变成一个完全混乱的方式太容易了,在那个方面,这取决于你(很可能是你的团队)。

That being said, it’s way too easy to turn it into a total mess, where that bound is, is up to you (and most probably your team).