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

前端 未结 3 1993
误落风尘
误落风尘 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:26

    I think, Java Optional is made for these type of use cases only - To avoid null checks and do operations via chaining & without the fear of NPE.

    I don't think you are overusing Optional.

    From the Official Article from Oracle

    The purpose of Optional is not to replace every single null reference in your codebase but rather to help design better APIs in which—just by reading the signature of a method—users can tell whether to expect an optional value. In addition, Optional forces you to actively unwrap an Optional to deal with the absence of a value; as a result, you protect your code against unintended null pointer exceptions.

提交回复
热议问题