15 ms·
Not to defend Java, but some examples seem just unfair: if (nullableVariable != null) { boolean success = nullableVariable.someMethodCall()
by psychoslave 7y ago
Not to defend Java, but some examples seem just unfair:
if (nullableVariable != null) {
boolean success = nullableVariable.someMethodCall()
if (success) {
return success
} else {
return fallbackIfNullMethodCall()
}
} else {
return fallbackIfNullMethodCall()
}
// Wouldn't that work just as well ?
if (nullableVariable != null) {
return nullableVariable.someMethodCall() || fallbackIfNullMethodCall();
}
return fallbackIfNullMethodCall();
Ok, that is still more cluter than the kotlin elvis operator, but more fair, isn't it?
- boring_twenties 7y ago100%, who the hell would write if (success) return success. Come on.
- afandian 7y agoHow would you handle the else clause?
- boring_twenties 7y agoThe comment I responded to has the correct way (return success || somethingelse).
- deleted 7y ago[deleted]
- RyEgswuCsn 7y agoWould this work? return Optional.ofNullable(nullableVariable).map(v -> v.someMethodCall()).orElse(false) ? true : fallbackIfNullMethodCall();
- AnyTimeTraveler 7y agoWhy not replace the tenary operator at the end with the method call in the or else block? return Optional.ofNullable(nullableVariable) .map(v::someMethodCall) .orElse(fallbackIfNullMethodCall); That also looks much easier to understand, at least to me.
- dukoid 7y agoreturn nullableVariable != null && nullableVariable.someMethodCall() || fallbackIfNullMethodCall()
- deleted 7y ago[deleted]