From deaf98d203c91c6bba80c6d2e321dd3fbe04180f Mon Sep 17 00:00:00 2001 From: Leonid Date: Mon, 3 Feb 2025 16:08:40 +0200 Subject: [PATCH 1/2] docs: updated Naked parameters --- src/param-naked.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/param-naked.md b/src/param-naked.md index 357621d..80e2c75 100644 --- a/src/param-naked.md +++ b/src/param-naked.md @@ -1,6 +1,6 @@ # Avoid Naked Parameters -Prefer using (Functional Options)[#functional-options] instead of naked parameters or split your function into a few ones: +Prefer using [Functional Options](#functional-options) instead of naked parameters or split your function into a few ones: From 9e0a6c346d426c8a906e2390eae3a7739a67cb12 Mon Sep 17 00:00:00 2001 From: GoncharovLeonid <50236144+GoncharovLeonid@users.noreply.github.com> Date: Mon, 3 Feb 2025 14:09:14 +0000 Subject: [PATCH 2/2] Auto-update style.md --- style.md | 37 ++++++++----------------------------- 1 file changed, 8 insertions(+), 29 deletions(-) diff --git a/style.md b/style.md index 3774b31..b1f1ba5 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:
BadGood
@@ -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),