Comment

Comment by jayvanguard

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.

parent jayvanguard 8 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton