From c036a5af551b96648311a5313d94c439aa222985 Mon Sep 17 00:00:00 2001 From: tfrench Date: Thu, 1 Aug 2024 19:50:19 +0000 Subject: [PATCH 1/5] give guidance to reduce scope of constants --- src/var-scope.md | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/var-scope.md b/src/var-scope.md index 362b651e..e901dbaf 100644 --- a/src/var-scope.md +++ b/src/var-scope.md @@ -1,6 +1,6 @@ # Reduce Scope of Variables -Where possible, reduce scope of variables. Do not reduce the scope if it +Where possible, reduce scope of variables and constants. Do not reduce the scope if it conflicts with [Reduce Nesting](nest-less.md). @@ -66,3 +66,36 @@ return nil
+ +Constants do not need to be global unless they are used in multiple functions or files. + + + + + +
BadGood
+ +```go +const ( + _defaultPort = 8080 + _defaultUser = "user" +) + +func Bar() { + fmt.Println("Default port", _defaultPort) +} +``` + + + +```go +func Bar() { + const ( + defaultPort = 8080 + defaultUser = "user" + ) + fmt.Println("Default port", _defaultPort) +} +``` + +
From 112031fc2592cdf86464407967c4c0c23eb994f7 Mon Sep 17 00:00:00 2001 From: tyler-french Date: Fri, 9 Aug 2024 14:42:52 +0000 Subject: [PATCH 2/5] Auto-update style.md --- style.md | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/style.md b/style.md index 21a1733a..90ee7055 100644 --- a/style.md +++ b/style.md @@ -3209,7 +3209,7 @@ be treated differently in different situations (such as serialization). ### Reduce Scope of Variables -Where possible, reduce scope of variables. Do not reduce the scope if it +Where possible, reduce scope of variables and constants. Do not reduce the scope if it conflicts with [Reduce Nesting](#reduce-nesting). @@ -3276,6 +3276,39 @@ return nil
+Constants do not need to be global unless they are used in multiple functions or files. + + + + + +
BadGood
+ +```go +const ( + _defaultPort = 8080 + _defaultUser = "user" +) + +func Bar() { + fmt.Println("Default port", _defaultPort) +} +``` + + + +```go +func Bar() { + const ( + defaultPort = 8080 + defaultUser = "user" + ) + fmt.Println("Default port", _defaultPort) +} +``` + +
+ ### Avoid Naked Parameters Naked parameters in function calls can hurt readability. Add C-style comments From b523b02edd976eaf6bbad674e9823a138e2840e5 Mon Sep 17 00:00:00 2001 From: Tyler French Date: Fri, 9 Aug 2024 14:44:56 -0500 Subject: [PATCH 3/5] Update var-scope.md Co-authored-by: Abhinav Gupta --- src/var-scope.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/var-scope.md b/src/var-scope.md index e901dbaf..b6892a99 100644 --- a/src/var-scope.md +++ b/src/var-scope.md @@ -93,7 +93,7 @@ func Bar() { defaultPort = 8080 defaultUser = "user" ) - fmt.Println("Default port", _defaultPort) + fmt.Println("Default port", defaultPort) } ``` From d90a1ea4ea4290d5da7ff8b5d3d8139c1ba7eb3b Mon Sep 17 00:00:00 2001 From: Tyler French Date: Fri, 9 Aug 2024 14:45:05 -0500 Subject: [PATCH 4/5] Update var-scope.md Co-authored-by: Abhinav Gupta --- src/var-scope.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/var-scope.md b/src/var-scope.md index b6892a99..f3ae6d5f 100644 --- a/src/var-scope.md +++ b/src/var-scope.md @@ -67,7 +67,8 @@ return nil -Constants do not need to be global unless they are used in multiple functions or files. +Constants do not need to be global unless they are used in multiple functions or files +or are part of an external contract of the package. From 4645b7c2170e45589b1333b0005b08a08b8be651 Mon Sep 17 00:00:00 2001 From: tyler-french Date: Fri, 9 Aug 2024 19:45:09 +0000 Subject: [PATCH 5/5] Auto-update style.md --- style.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/style.md b/style.md index 90ee7055..71db54c1 100644 --- a/style.md +++ b/style.md @@ -3276,7 +3276,8 @@ return nil
BadGood
-Constants do not need to be global unless they are used in multiple functions or files. +Constants do not need to be global unless they are used in multiple functions or files +or are part of an external contract of the package. @@ -3302,7 +3303,7 @@ func Bar() { defaultPort = 8080 defaultUser = "user" ) - fmt.Println("Default port", _defaultPort) + fmt.Println("Default port", defaultPort) } ```
BadGood