3 ms·
Even the simple if/else approach can be done more cleanly (i.e. without repeating the fallback call): boolean success = false; if (nullableVariable != null
by rococode 7y ago
Even the simple if/else approach can be done more cleanly (i.e. without repeating the fallback call):
boolean success = false;
if (nullableVariable != null) {
success = nullableVariable.someMethodCall();
}
if (success) {
return success;
} else {
return fallbackIfNullMethodCall();
}
- arnvidr 7y agoEven that seems way too much. The original would be much easier as: if (nullableVariable != null) { boolean success = nullableVariable.someMethodCall() if (success) { return success; } } return fallbackIfNullMethodCall(); And even that can be more concise if you prefer: if (nullableVariable != null && nullableVariable.someMethodCall()) { return true; } return fallbackIfNullMethodCall();
- nimchimpsky 7y agoWhy isn't anyone using the ternary operator ? return (nullableVariable != null && nullableVariable.someMethodCall()) ? true : fallbackIfNullMethodCall();
- posedge 7y agoThat's 9 lines of code vs. 3, and one additional variable. Cognitive load from lots of verbosity like this adds up.