Hi guys. My questions is about if developers today, in the everyday work, use Optional instead of null.
I ask it because I spend almost four years without working in a formal way as developer for a company and I don't know if people use this features of Java 8.
Before I was woking in average 2 years with Java 4, 6 and 7 and it's like there has been a lot of water under the bridge so far.
Totally unscientific wild-a$$ guess: No, most developers don't use Optional. I would guess more than 50% of Java developers being paid to write Java code will give you a blank stare back if you ask them that question.
Edit: I may sound very cynical but I'm guessing the above because most code I have seen in the past few years since Java 8 introduced Optional has either NOT been written to Java 8 or they are still returning null values from methods all over the place. They don't even return empty collections when they should. I see this kind of code all the time:
Sorry, but you can't help being cynical about Optional if programmers don't even have the common sense to design APIs that return at least an empty collection instead of null.
Thanks guys for your replies so far. And Junilu Lacar about you said, I agree haha. But I wasn't sure really and these are the big differences between we can find in a book and on the other hand, in real life.
I will attach a pic about what the book says about it (OCP Study guide).
Said that, I'm very grateful to have these books with me, They're the best I found.
Anyway I'd like to read opinions where people say if either this feature is a good tool, o not, and why. Beause maybe it's a very good feaure that it's not implemented because the low level coding of programmers.
Let me say, in my opinion (I discovered it 2hs ago) It's not so intuitive. So to understand how it works you should look the class in deep. So I prefer code more readable that so technical.
Like Junilu, I can't speak for "all developers" or even a small subset of them. But I have this application which was originally written nearly 10 years ago and which I use almost every day. It's still a work in progress but obviously it was written long before Optional existed. As new Java features come along I try to use them in the app, for example I changed it to use try-with-resources throughout. (About 60 changes.) And recently I decided to use Optional in one of my application updates, where null-handling used to be there. There's a lot more scope for using Optional, obviously, as there are several hundred classes in the application. So when I get some time I might try to change more of the null-handling code to use Optional instead -- there are still weird situations which very rarely cause NullPointerException.
posted 1 week ago
Hi Ritchie. The book is OCP study guide of Jeanne and Scott and the picture is from the page 185.
Won't you please? Please won't you be my neighbor? - Fred Rogers. Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop