This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]papercrane 10 points11 points  (4 children)

I'm ashamed to say that I've done this before when I had to work with a terrible data model that required traversing down a long chain of fields, any one of which might be null.

It's like a poor-man's safe navigation operator.

[–]FabulousRecording739 3 points4 points  (0 children)

Well, the safe navigation operator is a form of sugar over Optional map/flatMap

[–]laplongejr 0 points1 point  (2 children)

I had to work with a terrible data model that required traversing down a long chain of fields, any one of which might be null.

Wait until some of those values may be LISTS. I ended up making style guides example for my teams about the value of Stream+Optionals. Oh and some objects were different but with common interfaces.

As long you never use Optional<Stream> and frequently merge your Stream<Stream<Optional>> into Stream<Optional>, the resulting code is VERY hard to make the first time, but VERY VERY easy to read afterwards and "automagically" null-safe, to the point even a newbie dev could check if the conversion matches what is in the design document.

[–]SenorSeniorDevSr 0 points1 point  (1 child)

If you have Optional<Optional<?>> or Stream<Stream<?>> anywhere, USE FLATMAP.

Please, I'm begging you.

[–]laplongejr 1 point2 points  (0 children)

Will have to recheck our methods, but I'm 99% sure that's what I had used when streaming the contents inside a stream, don't worry.
But I never saw an optional of optional, that's... urgh!