News for hackers. Stuff that matters.
Re:Greppability is an underrated code metric by madeofpalk on Tuesday September 03, 2024 @04:41AM (#41432693)
The culture of single letter variables in golang, at least in the codebases I've seen, undoes this.
Re:Greppability is an underrated code metric by alienchow on Tuesday September 03, 2024 @06:44AM (#41433450)
Single 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.

Re:Greppability is an underrated code metric by VonGallifrey on Tuesday September 03, 2024 @05:02AM (#41432831)
The 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.

Re:Greppability is an underrated code metric by lelanthran on Tuesday September 03, 2024 @05:27AM (#41432988)
> 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.

Re:Greppability is an underrated code metric by iudqnolq on Tuesday September 03, 2024 @06:28AM (#41433358)
I 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.
Re:Greppability is an underrated code metric by marcosdumay on Tuesday September 03, 2024 @10:37AM (#41435397)
> 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.

Re:Greppability is an underrated code metric by lanstin on Tuesday September 03, 2024 @10:47AM (#41435484)
Zipf's law, right - these rules are a formalization of our brain's functionality with language.

Of course, with enough code, someone does everything.

Re:Greppability is an underrated code metric by kazinator on Tuesday September 03, 2024 @11:22AM (#41435843)
E.g. food and art are very important in Japan, so stomach is i and a drawing/painting is e.
Re:Greppability is an underrated code metric by BrandoElFollito on Tuesday September 03, 2024 @01:22PM (#41437083)
Food is very very important in France so we call it nourriture :)