Skip to content

Commit

Permalink
Merge pull request #1 from exaring/add-naked-returns
Browse files Browse the repository at this point in the history
Add-naked-returns
  • Loading branch information
FlorianLoch authored Dec 10, 2024
2 parents a3c169c + c020bd7 commit 289fa0d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
- [nil is a valid slice](slice-nil.md)
- [Reduce Scope of Variables](var-scope.md)
- [Avoid Naked Parameters](param-naked.md)
- [Avoid Naked Returns](naked-return.md)
- [Use Raw String Literals to Avoid Escaping](string-escape.md)
- Initializing Structs
- [Use Field Names to Initialize Structs](struct-field-key.md)
Expand Down
29 changes: 29 additions & 0 deletions src/naked-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Avoid Naked Returns

Naked returns hurt readability and make it harder for the IDE to help you (e.g. when highlighting variable usage).

Always list the return values explicitly.

<table>
<thead><tr><th>Bad</th><th>Good</th></tr></thead>
<tbody>
<tr><td>

```go
func foo() (x int, err error) {
x = 42
return
}
```

</td><td>

```go
func foo() (x int, err error) {
x = 42
return x, err
}
```

</td></tr>
</tbody></table>
31 changes: 31 additions & 0 deletions style.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
- [nil is a valid slice](#nil-is-a-valid-slice)
- [Reduce Scope of Variables](#reduce-scope-of-variables)
- [Avoid Naked Parameters](#avoid-naked-parameters)
- [Avoid Naked Returns](#avoid-naked-returns)
- [Use Raw String Literals to Avoid Escaping](#use-raw-string-literals-to-avoid-escaping)
- [Initializing Structs](#initializing-structs)
- [Use Field Names to Initialize Structs](#use-field-names-to-initialize-structs)
Expand Down Expand Up @@ -3332,6 +3333,36 @@ const (
func printInfo(name string, region Region, status Status)
```

### Avoid Naked Returns

Naked returns hurt readability and make it harder for the IDE to help you (e.g. when highlighting variable usage).

Always list the return values explicitly.

<table>
<thead><tr><th>Bad</th><th>Good</th></tr></thead>
<tbody>
<tr><td>

```go
func foo() (x int, err error) {
x = 42
return
}
```

</td><td>

```go
func foo() (x int, err error) {
x = 42
return x, err
}
```

</td></tr>
</tbody></table>

### Use Raw String Literals to Avoid Escaping

Go supports [raw string literals](https://golang.org/ref/spec#raw_string_lit),
Expand Down

0 comments on commit 289fa0d

Please sign in to comment.