I guess I'm the minority here, I prefer "if else" with the happy path inside the if.
Returning early gives me mental overhead of keeping the "elses" on my head, and doing it in Scala makes the code look more verbose.
Returning early gives me mental overhead of keeping the "elses" on my head, and doing it in Scala makes the code look more verbose.
For complex methods with branching logic you want prominently displayed, you'd end up with:
def myfunction(args) {
// check preconditions, return early
if (x) {
// happy path
} else {
// less happy path
}
}But if the else just contains a bunch of error handling you always have the option of wrapping it in a handler method and putting it near the top in a on-liner.
So break early.