Skip to content

Commit

Permalink
fix: naked returns chapter (#2716)
Browse files Browse the repository at this point in the history
  • Loading branch information
notanatol authored Dec 7, 2021
1 parent 2e9818d commit 238867f
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions CODINGSTYLE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- [Consistent Spelling](#consistent-spelling)
- [Code Formatting](#code-formatting)
- [Unused Names](#unused-names)
- [Naked returns and Named Parameters](#naked-returns-and-named-parameters)
- [Parallel Test Execution](#parallel-test-execution)
- [Group Declarations by Meaning](#group-declarations-by-meaning)
- [Make Zero-value Useful](#make-zero-value-useful)
Expand Down Expand Up @@ -113,6 +114,42 @@ func method(string) {

Note: there might be a linter to take care of this

## Naked returns and Named Parameters

Don't name result parameters just to avoid declaring a var inside the function; that trades off a minor implementation brevity at the cost of unnecessary API verbosity.

Naked returns are okay if the function is a handful of lines. Once it's a medium sized function, be explicit with your return values.

```go
func collect(birds ...byte) (ducks []byte) {
for _, bird := range birds {
if isDuck(bird) {
ducks = append(ducks, bird)
}
}

return
}
```

*Corollary:* it's not worth it to name result parameters just because it enables you to use naked returns. Clarity of docs is always more important than saving a line or two in your function.

Finally, in some cases you need to name a result parameter in order to change it in a deferred closure. That is always OK.

```go
func getID() (id int, err error) {
defer func() {
if err != nil {
err = fmt.Errorf("some extra info: %v", err)
}
}

id, err = /* call to db */

return
}
```

## Parallel Test Execution

Run tests in parallel where possible but don't forget about variable scope gotchas.
Expand Down

0 comments on commit 238867f

Please sign in to comment.