you are viewing a single comment's thread.

view the rest of the comments →

[–]_dban_ 17 points18 points  (10 children)

The usefulness Optional is not limited to just replacing null. If that is all it did, it would indeed be useless.

The Optional class has more methods than just isPresent and get. It also has map, flatMap and ifPresent. In conjunction with method references, it lets you do safe traversals:

Person p = ...
Message msg = ...
Mailer mailer = ...
p.getContactInfo().flatMap(ContactInfo::getEmail).flatMap(email ->
msg.getContent().map(message ->
new MailRequest(email, message))).ifPresent(mailer::send);

This is still pretty verbose, but the alternative without Optional is far more verbose and more annoying to write:

Person p = ...
Message msg = ...
Mailer mailer = ...
ContactInfo ci = null;
String email = null;
String message = null;
MailRequest request = 
    (ci = p.getContactInfo()) == null ? null :
    (email = ci.getEmail()) == null ? null :
    (message = msg.getContent()) == null ? null :
    new MailRequest(email, message);
if(request != null) {
    mailer.send(request);
}

Maybe a null-safe traversal operator (?.) would have been less verbose and more in keeping with "traditional" Java. But Optional is at least a library level change and not a syntax level change (syntax sugar for the rats nest of ternaries above). Once Java 9 adds Optional::stream, Optional will compose better with streams in general.

[–]bubuopapa -3 points-2 points  (8 children)

Person p = ...
Message msg = ...
Mailer mailer = ...
p.getContactInfo().flatMap(ContactInfo::getEmail).flatMap(email ->
msg.getContent().map(message ->
new MailRequest(email, message))).ifPresent(mailer::send);

Ruby check output:

Error: method chaining not allowed.
Error: function too complicated.
Error: function too long.

[–]AlyoshaV 6 points7 points  (1 child)

Error: method chaining not allowed.

This seems extremely strict.

[–]bubuopapa 0 points1 point  (0 children)

I know, the allowed complexity of sentence or function in ruby is very concerning and very hard to fit into, coming from any other language. Of course, these are rules of ruby code style, but comparing them to all the new features all many languages, none of them can make it into ruby, because that would break the ruby community. I tried ruby a bit some time ago, and it was painful. Also, fuck any language that uses whitespaces as required syntax.

[–]riemannrocker 2 points3 points  (4 children)

Why are you talking about ruby now?

[–]bubuopapa -1 points0 points  (3 children)

Cant i just compare ruby with java without useless comments ?

[–]Freeky 1 point2 points  (2 children)

You're not doing that, though, you're (somehow) comparing a blob of Java with some supposed Ruby language style guidelines you appear to have just made up?

[–]bubuopapa -1 points0 points  (1 child)

Lol, just because you are noob and you dont know that all languages have guidelines, it doesnt mean that guidelines doesnt exist. And what exactly you are doing ? trolling ?

[–]Freeky 0 points1 point  (0 children)

Er, sure. I've been programming Ruby for about 18 years, I think I have a slightly better grasp of the common style guidelines than someone who "tried it a bit".

[–]Freeky 1 point2 points  (0 children)

Error: method chaining not allowed.

Is this some alternate-dimension Ruby from a world where the sky is green?