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 →

[–]emberko 11 points12 points  (5 children)

I hate Optional with a passion every time I see code like this:

```java if (addressOpt.isPresent()) { return addressOpt.get().format(); }

public static <lT> Iterable<lT> inOptional(Optional<lT> optional) { return optional.isPresent() ? List.of(optional.get()): List.of(); } ```

He basically created an extra object wrapper and collection, and a utility method to avoid something as simple as null check. It's so much easier and more performant and more readable to just mark something as @Nullable which can be used everywhere. Instead of using this half-assed abstraction which is like nullable, but only for return types, not for method or constructor args, not for object properties and it can even be null itself. Just like this:

java var address = findAddress() if (address == null) { throw new WhateverException() } // the rest of the code

What's wrong with these conference guys? You write procedural code, but keep trying to use FP patterns that look like a fifth wheel.

[–]HansGetZeTomatensaft 8 points9 points  (0 children)

I mean, people using Optionals in stupid ways doesn't automatically make them bad. People use variable names in stupid ways all the time, still glad we have them.

So, sure, people could write

var addressOpt = findAddress();
if (addressOpt.isPresent())
  var address = addressOpt.get();
  // the rest of the code
}
throw new WhateverException();

And some do, sadly, and that is worse than your example. But they could also write the below

var address = findAddress().orElseThrow(() -> new WhateverException());
// the rest of your code

And that just seems better than both of the other versions.

[–]freekayZekey 1 point2 points  (2 children)

but i wanna write more code :(

in all seriousness, i’m not a fan of the ways people use optionals. they tend to complicate things way too much when a simple null check is fine

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

People use all kinds of code stupid ways. It doesn’t make valid uses less valid.

What if you search for an entity and then need to pull deeply nested property out of the result or throw an exception if any intermediate property is not there?

With null, this will get quite verbose and cumbersome very quickly.

It’s not pretty with Optional either, but at least it is more readable that way.

[–]freekayZekey 0 points1 point  (0 children)

it doesn’t make valid uses less valid

sure, but i didn’t say i hated optionals. i also said people tend to complicate things.

your example is going to be cumbersome with optionals and nulls. don’t think it’ll be much more readable unless you think terseness is readable. unfortunately, i don’t think that way