NHacker Next
  • new
  • past
  • show
  • ask
  • show
  • jobs
  • submit
Go Naming Conventions: A Practical Guide (alexedwards.net)
laserlight 2 hours ago [-]
> we use the identifier p to represent a value in the people slice — the range block is so small and tight that using a single letter name is clear enough.

No, it's not. When you see `p.Age`, you have to go back and find the body of the loop, see what it operates on and decipher what p stands for. When you see `person.Age`, you understand it. I've never understood what is gained by using `p` instead of spelling it out as `person`.

piekvorst 1 hours ago [-]
Long lines make reading rhythm uncomfortable (long jumps, prolonged eye movements) and long words make the text too dense and slow down the reading. It’s bad typography.

I have heard an idea that a good variable should be understood by just reading its name, out of context. That would make “ProductIndex” superior to “i”, which doesn't add any clarity.

Joker_vD 4 minutes ago [-]
A good variable name is the one that is understood by reading it in context, which is why you don't have names like "current_person" or "CurrentIndexOfProductBeingUpdated".
monooso 22 minutes ago [-]
I would argue that ambiguity and uncertainty slow down reading, and more importantly comprehension, far more than a few additional characters.
saghm 1 hours ago [-]
I've felt strongly for a while now that abbreviations should be "lossless" in order to be useful; it should be unambiguous now get back to the unabbreviated form. For whatever reason, people seem to love trying to optimize for character count with abbreviations that actually make things more confusing (like `res` in a context where it might mean either "response" or "result).

I just don't get the obsession with terseness when we have modern tooling. I don't type particularly fast, but autocomplete makes it pretty quick for me to type out even longer names, and any decent formatter will split up long lines automatically in a way that's usually sane (and in my experience, the times when it's annoying are usually due to something like a function with way too many arguments or people not wanting to put a subexpression in a separate variable because I guess they don't know that the compiler will just inline it) rather than the names being a few characters too many.

Meanwhile, pretty much everywhere I've worked has had at least some concerns about code reviews either already being or potentially becoming a burden on the team due to the amount of time and effort it takes to read through someone else's code. I feel like more emphasis on making code readable rather than just functional and quick to write would be a sensible thing to consider, but somehow it never seems to be part of the discussion.

ickyforce 37 minutes ago [-]
> and any decent formatter will split up long lines

Any decent editor can wrap long lines on demand. But it's even better not to have to do either of those if not necessary.

> I've felt strongly for a while now that abbreviations should be "lossless" in order to be useful

This is how we got lpszClassName. The world moved away from hungarian notation and even away from defining types for variables in some contexts (auto in cpp, := in Go, var in Java). Often it just adds noise and makes it harder to understand the code at a glance, not easier.

cookiengineer 1 hours ago [-]
I agree with this comment so much.

Tried to use the new slices package or comparables? It's a nightmare to debug, for no reason whatsoever. If they would've used interface names like Slice or Comparable or Stringable or something, it would have been so much easier.

The naming conventions are something that really fucks up my coding workflow, and it can be avoided 100% of the time if they would stop with those stupid variable names. I am not a machine, and there is no reason to make code intentionally unreadable.

qezz 3 hours ago [-]
I was surprised to see literally invalid names in the "bad" section, e.g. "Cannot start with a digit". Why even presenting this if it's rejected by the compiler?
0x696C6961 3 hours ago [-]
The example with the dash in it confused me as well.
nasretdinov 3 hours ago [-]
I like this article — short, accurate (which is somehow not a given these days...) and useful, just like Go language itself.
red_admiral 4 hours ago [-]
Another of mine: don't name a struct after an interface method that it's supposed to implement. If you have a package linearalgebra, then making a custom error type linearalgebra.LinearAlgebraError is too "chatty" but linearalgebra.Error will cause you pain if it implements "Error string()", as it probably should, and you decide to make a linearalgebra.MatrixSingularError that wraps a linearalgebra.Error to "inherit" its methods.

In the end, it ended up called linearalgebra.Err .

P.S Alex Edwards' "let's go" and "let's go further" are great books to get someone up to date with golang, just keep an eye on features that are newer than the book(s).

DauntingPear7 3 hours ago [-]
The booms receive regular updates. I got an email about an update to Let’s Go Further on 3/12 for Go 1.26
nacozarina 4 hours ago [-]
this seems anachronistic, written for a human artisan laboring over each naming choice directly

a modern approach would be a condensed ruleset passed to the ai model for the code it generates, and it should be straightforward to substitute different rulesets if you have a different taste in parochialisms.

johneth 4 hours ago [-]
> this seems anachronistic, written for a human artisan laboring over each naming choice directly

Some of us want to write well thought-through code, rather than letting an AI just spew poorly thought-through unmaintainable shit.

piekvorst 4 hours ago [-]
You can use this article to guide your choice of rulesets. And you still have to exert some artisan labor to develop any taste.

Parochialism here is saying “just use AI” in disguise.

ErroneousBosh 31 minutes ago [-]
> passed to the ai model for the code it generates

AI can't generate code. It only copies it (badly) from somewhere else.

jonathanstrange 2 hours ago [-]
You're making yourself redundant if you don't keep your actual programming skills sharp.
andersonpico 3 hours ago [-]
[flagged]
piekvorst 45 minutes ago [-]
[dead]
Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact
Rendered at 15:29:23 GMT+0000 (Coordinated Universal Time) with Vercel.