3 ms·
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
by wiradikusuma 9y ago
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.
- jayvanguard 9y agoI don't think you're in the minority. Whenever possible the happy path should be inside the if. The article seems to imply otherwise but on re-read I think he is just building the case for putting the simple, quick pre-condition type handling up front which generally removes the need for the if/then (which I agree with). 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.
- redwolf2 9y agoProblem is, you may build the if pyramid of death, if you branch too deep. So break early. https://www.youtube.com/watch?v=GIl_BNIJcFg https://www.youtube.com/watch?v=GIl_BNIJcFg