Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 8 additions & 29 deletions src/param-naked.md
Original file line number Diff line number Diff line change
@@ -1,49 +1,28 @@
# Avoid Naked Parameters

Naked parameters in function calls can hurt readability. Add C-style comments
(`/* ... */`) for parameter names when their meaning is not obvious.
Prefer using (Functional Options)[#functional-options] instead of naked parameters or split your function into a few ones:

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

```go
// func printInfo(name string, isLocal, done bool)
// func getInfo(name string, useCache bool)

printInfo("foo", true, true)
getInfo("foo", true)
```

</td><td>

```go
// func printInfo(name string, isLocal, done bool)
// func getInfo(name string)

printInfo("foo", true /* isLocal */, true /* done */)
// func getInfoUsingCache(name string)

getInfo("foo")
getInfoUsingCache("foo")
```

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

Better yet, replace naked `bool` types with custom types for more readable and
type-safe code. This allows more than just two states (true/false) for that
parameter in the future.

```go
type Region int

const (
UnknownRegion Region = iota
Local
)

type Status int

const (
StatusReady Status = iota + 1
StatusDone
// Maybe we will have a StatusInProgress in the future.
)

func printInfo(name string, region Region, status Status)
```
37 changes: 8 additions & 29 deletions style.md
Original file line number Diff line number Diff line change
Expand Up @@ -3375,54 +3375,33 @@ func Bar() {

### Avoid Naked Parameters

Naked parameters in function calls can hurt readability. Add C-style comments
(`/* ... */`) for parameter names when their meaning is not obvious.
Prefer using (Functional Options)[#functional-options] instead of naked parameters or split your function into a few ones:

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

```go
// func printInfo(name string, isLocal, done bool)
// func getInfo(name string, useCache bool)

printInfo("foo", true, true)
getInfo("foo", true)
```

</td><td>

```go
// func printInfo(name string, isLocal, done bool)
// func getInfo(name string)

printInfo("foo", true /* isLocal */, true /* done */)
// func getInfoUsingCache(name string)

getInfo("foo")
getInfoUsingCache("foo")
```

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

Better yet, replace naked `bool` types with custom types for more readable and
type-safe code. This allows more than just two states (true/false) for that
parameter in the future.

```go
type Region int

const (
UnknownRegion Region = iota
Local
)

type Status int

const (
StatusReady Status = iota + 1
StatusDone
// Maybe we will have a StatusInProgress in the future.
)

func printInfo(name string, region Region, status Status)
```

### Use Raw String Literals to Avoid Escaping

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