4 ms·
The culture of single letter variables in golang, at least in the codebases I've seen, undoes this.
by madeofpalk 2y ago
The culture of single letter variables in golang, at least in the codebases I've seen, undoes this.
- VonGallifrey 2y agoThe way I have seen this is that single letter variables are mostly used when declaration and (all) usages are very close together. If I see a loop with i or k, v then I can be fairly confident that those are an Index or a Key Value pair. Also I probably don't need to grep them since everything interacting with these variables is probably already on my screen. Everything that has a wider scope or which would be unclear with a single letter is named with a more descriptive name. Of course this is highly dependent on the people you work with, but this is the way it works on projects I have worked on.
- lelanthran 2y ago> The culture of single letter variables in golang, at least in the codebases I've seen, undoes this. The convention, not just in Go, is that the smaller the scope, the smaller the variable reference. So, sure, you're going to see single-letter variables in short functions, inside short block scopes, etc, but that is true of almost any language. I haven't seen single-letter variables in Go that are in a scope that isn't short. Of course, this could just mean that I haven't seen enough of other peoples Go source.
- marcosdumay 2y ago> that is true of almost any language You'd be surprised how often language-local cultures break that rule on either side. And a few times it's even an improvement.
- iudqnolq 2y agoI like using l for logger and db for database client/pool/handle even if there's a wider scope. And if the bulk of a file is interacting with a single client I might call that c.
- lanstin 2y agoZipf's law, right - these rules are a formalization of our brain's functionality with language. Of course, with enough code, someone does everything.
- kazinator 2y agoE.g. food and art are very important in Japan, so stomach is i and a drawing/painting is e.
- BrandoElFollito 2y agoFood is very very important in France so we call it nourriture :)
- alienchow 2y agoSingle letter variables in Golang are to be used in small, local contexts. Akin to the throwaway i var in for loops. You only grep the struct methods, the same way no one greps 'this' or 'self'. The code bases you've been reading, and even some of the native libraries, don't do it properly. Probably due to legacy reasons that wouldn't pass readability approvals nowadays.