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

前端 未结 3 1957
误落风尘
误落风尘 2021-01-07 04:07

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

i had a conversation with group of collegues about Optional.of() method. currently one of the

3条回答
  •  说谎
    说谎 (楼主)
    2021-01-07 04:33

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

    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.

    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).

提交回复
热议问题