Try to handle errors first so the happy path is clear #11

Closedrenannprado opened this issue 7 years ago
renannprado commented 7 years ago

Do this:

err := doSomething()

if err != nil {
	// handle the error
}

// happy path continues

err = doSomethingElse()

if err != nil {
	// handle the error
}

// happy path continues

Don't do this:

err := doSomething()

if err == nil {
	// happy path continues

	err = doSomethingElse()

	if err == nil {
		// happy path continues
	} else {
		// handle the error
	}
} else {
	// handle the error
}
Write Preview Markdown
dmitshur commented 7 years ago ยท edited

Hey @renannprado, thanks for contributing the first proposal!

I think I've seen that one already, it's either a part of Effective Go or CodeReviewComments. If so, there's no point in repeating it here, since this list serves to augment those 2 documents.

P.S. To get syntax highlighting, use the language name "Go" in the fenced code block, not "golang".

Write Preview Markdown
dmitshur commented 7 years ago

Yep, found it right here:

https://github.com/golang/go/wiki/CodeReviewComments#indent-error-flow

I'll close this then since it's a duplicate, is that okay?

Write Preview Markdown
renannprado commented 7 years ago

Sorry about that. I didn't pay attention that you had this one already in that other link. Yeah, feel free to even delete this one if you want.

Thanks!

Write Preview Markdown
dmitshur commented 7 years ago

No worries, thank you anyway! I appreciate it. :)

Write Preview Markdown
dmitshur closed this 7 years ago
to comment.