使用Optional.of()方法进行方法链接是个好主意吗? [英] Is it good idea to use Optional.of() method to make method chaining?

查看:163
本文介绍了使用Optional.of()方法进行方法链接是个好主意吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it good idea to use Optional.of() method to make method chaining ?

我与一组关于Optional.of()方法的同事进行了对话。
目前其中一个项目是他们使用Optional.of()方法来
实现链接函数编程风格的方法。

i had a conversation with group of collegues about Optional.of() method. currently one of the project they are using Optional.of() method to achieve the method chaining functional programming style .

这里我给予示例示例:

    TxResponse initTxDataResponse = gateway.initiateTx(initTxDataRequest);
    Optional.of(initTxDataResponse)
            .map(initTxDataResponse::getReturnCode)
            .filter(code -> ResponseCode.SUCCESS.getCode().equals(code))
            .map(code -> {
                // doing some business logic ,we are not
                return code;
            }).orElseThrow(() -> new NoTransactionFound());

我觉得我们过度使用Optional.of()方法

I feel we are overusing of the Optional.of() method

使用Optional.of()方法在jdk 8中实现函数式编程风格是不错的做法?

Is it good practice use the Optional.of() method to achieve functional programming style in jdk 8 ?

案例2:

TxResponse response = transactionRepository.getTransacationById(TransactionId).elseThrow(() -> ResourceNotFoundException());

Optional.of(response)
        .map(response::getReturnCode)
        .filter(code -> ResponseCode.SUCCESS.getCode().equals(code))
        .map(code -> {
            // doing some business logic ,we are not
            return code;
        }).orElseThrow(() -> new RuntimeException());

在这种情况下,需要Optional.of()来实现函数式编程风格?

In this case Optional.of() is required to achieve functional programming style ?

推荐答案

如果你不介意没有代码的答案,只需要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).

这篇关于使用Optional.of()方法进行方法链接是个好主意吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆