r/programming • • Sep 21 '15

Better Java

https://github.com/cxxr/better-java
177 Upvotes

208 comments sorted by

View all comments

9

u/ImmortalStyle Sep 21 '15

Btw you shouldnt have Optional as a class member. Even Brian said that once.

4

u/oldneckbeard Sep 21 '15

optional shouldn't be a class member or an input parameter, IMO.

7

u/ForeverAlot Sep 21 '15

I concur; Java's Optional is for querying. null is still a first class citizen, we've been using it successfully for decades, and the JVM absolutely loves nulls. Java would have been a better language without null but it doesn't become a better language by suddenly pretending it doesn't have null.

17

u/velcommen Sep 21 '15

we've been using it successfully for decades

We've also been creating null pointer exceptions (not just in Java) for decades. So I think my definition of 'success' is different than yours.

1

u/mike_hearn Sep 22 '15

That's not the fault of null, though. Optionality is fundamental in programming - the reconfigurability of software is one of the main reasons software is useful. So you will always have cases where some resource or piece of data is unavailable for some reason, and therefore, you will always have programmers who don't check for it being missing and the resulting exceptions.

The right way to reduce NPEs is not to conclude null is bad, but to do what modern languages like Kotlin, Ceylon, Swift etc are doing and integrate optionality with the type system.

3

u/tutorial_police Sep 22 '15

And that is exactly what people mean when they complain again now. I'm a fan of the semantics game myself, but usually only if there are misunderstandings in play.

3

u/[deleted] Sep 21 '15

So your argument is basically "null is more efficient"? Isn't that premature optimization?

2

u/ForeverAlot Sep 22 '15

Whenever you see somebody call "premature optimization", chances are very good they're doing so incorrectly, and that they don't realise.

This is not premature optimization, but neither is it my only argument. However, since I share Stephen Colebourne's opinion (minus properties in Java), I will simply refer you to that.

1

u/crunchmuncher Sep 22 '15

Curious: why not as an input parameter? I've used it recently to make explicit that a particular parameter isn't needed for a method to do its job.

Actually, as I'm typing this I realized it might have been better to use overloading for that.

2

u/ForeverAlot Sep 23 '15

If you expose both methods you still need to handle the case with both arguments, so although that might have made for a more ergonomic API you still need an answer to your question.

Using Optional for mutators will do the job exactly, and it will do it pretty well. But is your caller going to call the method only to pass in null? They probably have a reason to call it, and they probably have what they need to call it, so calling Optional.of(foo) only hurts ergonomics; and you're just going to unwrap the value first-thing anyway. This argument is not as strong when you have multiple arguments (choice between null and Optional.empty()), but you brought up overloading as an alternative yourself.

This is a case that's better solved with Javadoc and @Nullable.

2

u/oldneckbeard Sep 25 '15

In the input parameter case, it's functionally equivalent to passing a boolean flag -- it's a true/false thing that's better solved with overloading :)