@__pity__ @chavacava well, a single letter that you have tp scroll to get to the definition is bad. Really bad.
The good practice encourages to use single letter variable. But if, and ONLY if the use of the variable is on the same screen of code (so around 25-30 LOC depending on your editor).
If it's more then, then it is harmful.
@__pity__ @chavacava as I said earlier, needing to scroll to know the definition of a single letter variable is bad. So in that case, it is bad practice.
https://go.dev/wiki/CodeReviewComments#variable-names
https://go.dev/wiki/CodeReviewComments#receiver-names
And the longer, more explained version of it: https://google.github.io/styleguide/go/decisions#variable-names
@__pity__ @chavacava As a TL;DR:
1 letter variable name is OK only if:
- receiver name
- well known var names in Go context: t for testing.T, b for []byte, w io.Writer or http.ResponseWriter, r io.Reader or *http.Request, i for loop index, …
- first letter of a type if it is used in the very next lines of code. If you need to scroll, then you need to give a more explicit name, otherwise, you lose your fellow coders.
And that's about it.
```
b, _ := io.ReadAll(r.Body)
var u User
json.Unmarshal(b, &u)
posts, _ := svc.Posts.FindByAuthor(u.ID)
for _, p := range posts {
p.Author = u.Name
}
````
VS
```
responseByteSlice, _ := io.ReadAll(httpResponse.Body)
var databaseUser User
json.Unmarshal(responseByteSlice, &databaseUser)
postSlice, _ := service.PostRepository.FindPostsByAuthor(u.ID)
for _, post := range postSlice {
post.Author = databaseUser.Name
}
```
@__pity__ @chavacava as long as it's not the 2nd version, I'm OK :p
For me, it's when the lines get very long without much context added to it that it's getting harder to grasp. Between 1 and 6 characters is really fine when used closed to its definition.
Also, when your function starts spanning way too many lines, you would need more descriptive names, but also, maybe the function needs to be refactored. (I'm not in the camp of functions of 20 LOC or less, though).
Again, it's more about mental load. And character load could increase mental load without giving you more knowledge of the function under analysis.