You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -75,14 +75,14 @@ I have included shortcuts for Intellij/Goland, which my colleagues and I use. Wh
75
75
If you create a variable, only for it to be passed on to another method/function:
76
76
77
77
```go
78
-
url := baseURL+"/user/"+id
78
+
url := baseURL + "/user/" + id
79
79
res, err := client.Get(url)
80
80
```
81
81
82
82
Consider inlining it (`command+option+n`) *unless* the variable name adds significant meaning.
83
83
84
84
```go
85
-
res, err := client.Get(baseURL+"/user/"+id)
85
+
res, err := client.Get(baseURL + "/user/" + id)
86
86
```
87
87
88
88
Don't be _too_ clever about inlining; the goal is not to have zero variables and instead have ridiculous one-liners that no one can read. If you can add significant naming to a value, it might be best to leave it be.
@@ -130,7 +130,8 @@ func main() {
130
130
api3Client:= http.Client{
131
131
Timeout: 1 * time.Second,
132
132
}
133
-
// etc
133
+
//etc
134
+
}
134
135
```
135
136
136
137
We are setting up some HTTP clients for our application. There are some _magic values_ here, and we could DRY up the `Timeout` by extracting a variable and giving it a meaningful name.
@@ -152,6 +153,7 @@ func main() {
152
153
Timeout: timeout,
153
154
}
154
155
// etc..
156
+
}
155
157
```
156
158
157
159
We no longer have a magic value; we have given it a meaningful name, but we have also made it so all three clients **share the same timeout**. That _may_ be what you want; refactors are quite context-specific, but it's something to be wary of.
Now, we can extract the creation of the JSON payload into a function using the extract method refactor (`command+option+m`) to remove the noise from the method.
A refactoring technique you could apply here is, if a value is being created **that is not dependant on the arguments to the method**, then you can instead create a _field_ in your type and calculate it in your constructor function.
This is a small improvement, but it undoubtedly reads better. If you are well-practised, this kind of improvement will barely take you a minute, and so long as you have applied TDD well, you'll have the safety net of tests to ensure you're not breaking anything. These continuous minor improvements are vital to the long-term health of a codebase.
0 commit comments