4 ms·
Great list, even if I disagree with many of the ratings! But astonished that Optional isn't mentioned either there or in the comments. A second way to represen
by hdpe 1y ago
Great list, even if I disagree with many of the ratings!
But astonished that Optional isn't mentioned either there or in the comments. A second way to represent no-value, with unclear and holy-war-ushering guidance on when to use, and the not exactly terse syntax I see everywhere:
Optional<Ick> ickOpt = Optional.ofNullable(ickGiver.newIck());
ickOpt.flatMap(IckWtfer::wtf).ifPresentOrElse((Wtf wtf) -> unreadable(wtf)), () -> { log.warn("in what universe is this clearer than a simple if == null statement?!"); });
- skribanto 1y agoWell, it can make some chained function composition easier. Ick1 result1 = potentiallyNull1(); Ick2 result2 = (result1 == null) ? null : potentiallyNull2(result1); Ick3 result3 = (result2 == null) ? null : potentiallyNull3(result2); vs Ick3 result3 = potentiallyNone1() .flatMap(potentiallyNone2) .flatMap(potentiallyNone3); You could maybe move the null check inside the method in the former and it cleans it up a bit, but in the latter you can have methods that are explicitly marked as taking NonNull in their type signature which is nice.
- munksbeer 1y agoI don't grumble about Java much, I make my living with it and enjoy it. But Optional is a bane. We use NullAway and I just never use Optional unless it really, really makes sense.