diff --git a/src/param-naked.md b/src/param-naked.md index c1b969e..357621d 100644 --- a/src/param-naked.md +++ b/src/param-naked.md @@ -1,7 +1,6 @@ # 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: @@ -9,41 +8,21 @@ Naked parameters in function calls can hurt readability. Add C-style comments
BadGood
```go -// func printInfo(name string, isLocal, done bool) +// func getInfo(name string, useCache bool) -printInfo("foo", true, true) +getInfo("foo", true) ``` ```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") ```
- -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) -``` diff --git a/style.md b/style.md index 3774b31..5735c1c 100644 --- a/style.md +++ b/style.md @@ -3375,8 +3375,7 @@ 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: @@ -3384,45 +3383,25 @@ Naked parameters in function calls can hurt readability. Add C-style comments
BadGood
```go -// func printInfo(name string, isLocal, done bool) +// func getInfo(name string, useCache bool) -printInfo("foo", true, true) +getInfo("foo", true) ``` ```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") ```
-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),