Skip to content

Commit e89befe

Browse files
committed
fix formatter panic
1 parent 68c3afc commit e89befe

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

refactoring-checklist.md

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ What if you want to change the signature of a method?
3030

3131
```go
3232
func (b BirthdayGreeter) WishHappyBirthday(age int, firstname, lastname string, email Email) {
33-
// some fascinating emailing code
33+
// some fascinating emailing code
3434
}
3535
```
3636

@@ -75,14 +75,14 @@ I have included shortcuts for Intellij/Goland, which my colleagues and I use. Wh
7575
If you create a variable, only for it to be passed on to another method/function:
7676

7777
```go
78-
url := baseURL+"/user/"+id
78+
url := baseURL + "/user/" + id
7979
res, err := client.Get(url)
8080
```
8181

8282
Consider inlining it (`command+option+n`) *unless* the variable name adds significant meaning.
8383

8484
```go
85-
res, err := client.Get(baseURL+"/user/"+id)
85+
res, err := client.Get(baseURL + "/user/" + id)
8686
```
8787

8888
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() {
130130
api3Client := http.Client{
131131
Timeout: 1 * time.Second,
132132
}
133-
// etc
133+
//etc
134+
}
134135
```
135136

136137
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() {
152153
Timeout: timeout,
153154
}
154155
// etc..
156+
}
155157
```
156158

157159
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.
@@ -190,6 +192,8 @@ func (ws *WidgetService) CreateWidget(name string) error {
190192
url,
191193
bytes.NewBuffer([]byte(`{"name": "`+name+`"}`)),
192194
)
195+
// etc
196+
}
193197
```
194198

195199
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.
@@ -202,6 +206,8 @@ func (ws *WidgetService) CreateWidget(name string) error {
202206
url,
203207
createWidgetPayload(name),
204208
)
209+
// etc
210+
}
205211
```
206212

207213
Public methods and functions should describe *what* they do rather than *how* they do it.
@@ -235,7 +241,7 @@ Methods often have to create value and use them, like the `url` in our `CreateWi
235241
```go
236242
type WidgetService struct {
237243
baseURL string
238-
client *http.Client
244+
client *http.Client
239245
}
240246

241247
func NewWidgetService(baseURL string) *WidgetService {
@@ -252,6 +258,8 @@ func (ws *WidgetService) CreateWidget(name string) error {
252258
url,
253259
createWidgetPayload(name),
254260
)
261+
// etc
262+
}
255263
```
256264

257265
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.
@@ -267,8 +275,8 @@ func NewWidgetService(baseURL string) *WidgetService {
267275
Timeout: 10 * time.Second,
268276
}
269277
return &WidgetService{
270-
createWidgetURL : baseURL + "/widgets",
271-
client: &client,
278+
createWidgetURL: baseURL + "/widgets",
279+
client: &client,
272280
}
273281
}
274282

@@ -278,6 +286,8 @@ func (ws *WidgetService) CreateWidget(name string) error {
278286
ws.createWidgetURL,
279287
createWidgetPayload(name),
280288
)
289+
// etc
290+
}
281291
```
282292

283293
By moving them to construction time, you can simplify your methods.
@@ -295,6 +305,9 @@ func (ws *WidgetService) CreateWidget(name string) error {
295305
url,
296306
bytes.NewBuffer(payload),
297307
)
308+
// etc
309+
}
310+
298311
```
299312

300313
With a few basic refactors, driven almost entirely using automated tooling, we resulted in
@@ -306,6 +319,8 @@ func (ws *WidgetService) CreateWidget(name string) error {
306319
ws.createWidgetURL,
307320
createWidgetPayload(name),
308321
)
322+
// etc
323+
}
309324
```
310325

311326
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

Comments
 (0)