Hacker News new | ask | show | jobs
by jayvanguard 3006 days ago
I 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.

1 comments

Problem 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