News for hackers. Stuff that matters.
Re:Practical Go: Real-world advice for writing maintainable ... by pcwalton on Thursday February 21, 2019 @02:50PM (#19219960)
Yeah, I used to go by this advice, and I found the maintainability of my code dramatically increased when I typed out full names. I don't even use "i" for loop variables anymore. If the length is a problem, invest in an editor with autocomplete.

Well-known abbreviations are fine, like "iter" and "prev", but single-letter variable names notoriously impede readability for me.

Re:Practical Go: Real-world advice for writing maintainable ... by dilap on Thursday February 21, 2019 @02:58PM (#19220040)
I feel like I have the opposite problem.

If the name is more than a few characters long, it starts to become non-instantaneous to recognize it. Things get much easier to follow with visually-instantly-recognizable symbols.

So in conditions where a variable is used over a short area in the code (or where it's used _constantly_ over a wide area), I prefer short variables.

Re:Practical Go: Real-world advice for writing maintainable ... by Cthulhu_ on Friday February 22, 2019 @04:10AM (#19224035)
I think you're identifying the core issue here, that is, frequently vs infrequently read code. I'd argue that if you've spent enough time in go, variables like `i`, `conf` and `ctx` will become very recognisable and easy to skim over, if they're always used in the same context.

If it's less "boilerplate" code, I'd go for more descriptive names. I think.

I like the sorta described "rule" in this document; the longer the variable is used, the more descriptive it should be.

Re:Practical Go: Real-world advice for writing maintainable ... by hackinthebochs on Thursday February 21, 2019 @05:02PM (#19220942)
Totally agree. If you have to scroll up to be reminded what some variable means, a longer name is good. But if its usage is a few lines away from its declaration then there's no reason to add visual noise to your code.
Re:Practical Go: Real-world advice for writing maintainable ... by shaan7 on Thursday February 21, 2019 @10:14PM (#19222807)
Its easier said than done though. We started following this few years back but then while it was "few lines away" originally, code evolved and now there are parts where its 20+ lines away. This means that short var names need to be continuously "enlarged". Well, then why not start with a "medium" name and avoid all that headache? ctx is a good enough compromise between c and context. (c can be client, config, context, certificate ... you get the point).
Re:Practical Go: Real-world advice for writing maintainable ... by yawboakye on Friday February 22, 2019 @07:49AM (#19224850)
Rarely do you have a case where a variable outgrows its simple name (and value) and you keep it the same. Even when a function adopts new characteristics it's advisable to change it's name or make a new one. If you initially had variable `i`, created and destroyed in 5 lines of code but that has grown to 20+ lines, I'd recommend you (1) not add the 15+ if it could be put in a function (you get to name that section of the code), or (2) rename the variable. But a 5-line code that becomes 20+ lines is both strange and interesting.
Re:Practical Go: Real-world advice for writing maintainable ... by Cthulhu_ on Friday February 22, 2019 @04:11AM (#19224036)
Well as the article states, one-lettered variable names should only really be used in tight loops; `ctx` is a better name for e.g. a function argument.
Re:Practical Go: Real-world advice for writing maintainable ... by burke on Friday February 22, 2019 @09:57AM (#19225829)
This wouldn’t likely be hard to write a linter for: is the name 1-2 letters other than a method receiver, “i” or maybe “j” and is referenced more than 5 lines from where it’s declared? Lint warning.
Re:Practical Go: Real-world advice for writing maintainable ... by lugg on Saturday February 23, 2019 @02:41AM (#19232247)
I tend to agree here but I limit that to block scope as a general rule, I also only bother with really common concepts.

id identifier

i loop counter

j inner loop counter

c general purpose non loop counter

a, b, comparator methods

k, v, key value setters, array iteration

e, event, error or exception. Contextual rule but rarely collides.

Other than those I pretty much always write entire full word names. If it's abbreviated or shorthand it likely sounds funny in my head, or it leads to ambiguation. If I need to even ask / recall for a split second it's not good enough.

Programmers incorrectly place far too much weight on time to type. I write like ten lines of code on a good day. It's just not important. I want readable code that is clear and concise as possible. Every branch, block or shorthand variable raises a question I have to think about before moving on.

Clever code is why I call ten lines of code a good day. It sure as shit isn't my typing speed.

Re:Practical Go: Real-world advice for writing maintainable ... by mntmoss on Thursday February 21, 2019 @06:12PM (#19221548)
Dense code that benefits from dense variables(which describes a lot of "pure algorithms" work) I often approach by aliasing the variables to shorter ones. It's all in the same body so the context is not especially hard to lose.
Re:Practical Go: Real-world advice for writing maintainable ... by ivanhoe on Friday February 22, 2019 @08:51AM (#19225227)
IMHO short is OK as long as the name makes sense and is easy to connect to the real meaning. For instance "msg" is perfectly valid name for a message var, and even "m" in some short snippet of code is obvious, but calling it "a" or "x" or something completely unrelated like that is a bad idea. In matter of fact calling it something too neutral like "data" can also be as bad if it's not clear which data you mean. Context is everything in making it easy to understand.
Re:Practical Go: Real-world advice for writing maintainable ... by wnevets on Thursday February 21, 2019 @05:03PM (#19220954)
For me Config just isn't one of those cases, HackerNewsOnYCombinatorMessageBoard on the other hand becomes an opaque mess to eyes when it is mixed with other variables of similar length and complexity.
Re:Practical Go: Real-world advice for writing maintainable ... by apoorvgarg on Thursday February 21, 2019 @04:23PM (#19220703)
Regarding single-letter variables, mathematical functions might be an exception. I think writing func gcd(a, b int) int {...} is better than other alternatives. There is simply no need to assign any more meaning to the arguments other than their type.
Re:Practical Go: Real-world advice for writing maintainable ... by Cthulhu_ on Friday February 22, 2019 @04:12AM (#19224041)
Maybe if you - and more importantly, everyone that will read the code in the future - are comfortable with domain-specific expressions like that. It depends on the audience really.

As an extreme example, scalaz is similarly a very specialized DSL. Or in my personal experience, functional constructions like map, flatMap, foldLeft and reduce (which I never learned in school).

Re:Practical Go: Real-world advice for writing maintainable ... by bnolsen on Thursday February 21, 2019 @10:32PM (#19222878)
no, it's not. bigger deal is that single letter vars make it much harder to search for variable usage in files using arbitrary editors.
Re:Practical Go: Real-world advice for writing maintainable ... by coldtea on Friday February 22, 2019 @12:37AM (#19223380)
Don't use arbitrary editors, or editors that don't support "search for standalone identifier" (e.g. surrounded by space, in (), with ; after etc.".
Re:Practical Go: Real-world advice for writing maintainable ... by apoorvgarg on Thursday February 21, 2019 @11:36PM (#19223163)
What would be the signature of your gcd function ?
Re:Practical Go: Real-world advice for writing maintainable ... by base698 on Thursday February 21, 2019 @11:59PM (#19223259)
Numerator, denominator?
Re:Practical Go: Real-world advice for writing maintainable ... by mrgriffin on Friday February 22, 2019 @04:54AM (#19224203)
For GCD? I would find those names very misleading since semantically the order of the arguments to GCD is irrelevant (even if in the implementation you typically mod by b there's no reason you couldn't mod by a).
Re:Practical Go: Real-world advice for writing maintainable ... by sacado2 on Friday February 22, 2019 @05:36AM (#19224351)
This is not mathematically correct. Those names would be pretty misleading, actually.
Re:Practical Go: Real-world advice for writing maintainable ... by coldtea on Friday February 22, 2019 @12:37AM (#19223384)
Good luck with any bigger equation then
Re:Practical Go: Real-world advice for writing maintainable ... by zbobet2012 on Thursday February 21, 2019 @05:21PM (#19221086)
I generally find if you need multi letter variable names it means your function or scope is to long, or manipulating to many things. It's a nice little red flag for me.

Up to four or five single letter variables is pretty trivial to remember. Especially when three of those are i, j, and k. More than 6 or 7 rapidly becomes painful. But if you are manipulating 6 or 7 variables _in the same scope_ you are doing to much.

I also agree with Pikes comments about typography here: http://doc.cat-v.org/bell_labs/pikestyle

Re:Practical Go: Real-world advice for writing maintainable ... by pcwalton on Thursday February 21, 2019 @05:57PM (#19221411)
I could not disagree more. When I'm skimming code, I want to immediately know what a variable means. I don't want to go cross-reference elsewhere.
Re:Practical Go: Real-world advice for writing maintainable ... by zbobet2012 on Thursday February 21, 2019 @06:49PM (#19221788)
If you have to go "cross-reference" elsewhere you are modifying something "to far away" from you. That's a giant sign of spaghetti code.

Also names vary with there contextual scope. Larger scopes mean longer names generally. Russ Cox gives a succinct description here: https://research.swtch.com/names

  A name's length should not exceed its information content.
  For a local variable, the name i conveys as much information as 
  index or idx and is quicker to read. Similarly, i and j are a 
  better pair of names for index variables than i1 and i2 
  (or, worse, index1 and index2), because they are easier to
  tell apart when skimming the program. Global names must convey
  relatively more information, because they appear in a larger
  variety of contexts. Even so, a short, precise name can say more
  than a long-winded one: compare acquire and take_ownership. 
  
  Make every name tell.
Variables should generally have short scope (we don't want a lot of global, or even package level variables). So _variable names_ in particular should be short.
Re:Practical Go: Real-world advice for writing maintainable ... by bpicolo on Thursday February 21, 2019 @09:13PM (#19222544)
> For a local variable, the name i conveys as much information as index or idx and is quicker to read

This is only true because i is a specific, common abbreviation for index. When writing arbitrary glue code, a single letter variable would be a meaningless abbreviation without shared context. If you encounter "i" and it doesn't mean "index of a for loop", you're going to be taking additional time parsing meaning.

Re:Practical Go: Real-world advice for writing maintainable ... by 12298765 on Friday February 22, 2019 @02:03AM (#19223662)
An example where I disagree, and use 1-letter names daily: arrow functions in both Java and JS.

`usersList.stream().forEach(u -> someSet.add(u))`

It's immediately obvious that u is a user in usersList. I realize that it's debatable if u is really more readable than spelling out user, but I prefer it, and I don't think anyone is going to be confused by it. If the chain does get really long, also, I will spell it out explicitly.

Re:Practical Go: Real-world advice for writing maintainable ... by irishsultan on Friday February 22, 2019 @05:16AM (#19224273)
Shouldn't you be able to write that as `usersList.stream().forEach(someSet.add)`

For Javascript at least that would work.

Re:Practical Go: Real-world advice for writing maintainable ... by leshow on Friday February 22, 2019 @09:04AM (#19225347)
I haven't written Java since like java 5, but I believe java 8 has syntax for this as well:

`usersList.stream().forEach(someSet::add)`

Re:Practical Go: Real-world advice for writing maintainable ... by bpicolo on Friday February 22, 2019 @09:49AM (#19225746)
Sure, for a lambda I think it’s arbitrary. For larger functions / classes it matters
Re:Practical Go: Real-world advice for writing maintainable ... by arvinsim on Thursday February 21, 2019 @09:43PM (#19222680)
I disagree. I am pretty sure that there's some code that is short and simple but you want to have descriptive names anyway like financial related computations.
Re:Practical Go: Real-world advice for writing maintainable ... by coldtea on Friday February 22, 2019 @12:40AM (#19223392)
>I generally find if you need multi letter variable names it means your function or scope is to long, or manipulating to many things. It's a nice little red flag for me.

Many times your function should be longer, rather than shorter.

Short methods and functions used just for the sake of being short just move the complexity of understanding in the interaction between them, making logic harder to follow an algorithm when it could have been all in the same place (for related functionality of course, I don't advocate having a function do 2 different irrelevant things).

Re:Practical Go: Real-world advice for writing maintainable ... by ori_b on Thursday February 21, 2019 @07:50PM (#19222124)
Readability is a problem -- long variable names are harder to skim, and rapidly begin to blur together for me.
Re:Practical Go: Real-world advice for writing maintainable ... by bdamm on Thursday February 21, 2019 @10:34PM (#19222891)
Agreed. Step through the code in a debugger if you really want to understand what it's doing. Even unreproducible problems with production code can succumb to raw understanding gained from stepped test cases.
Re:Practical Go: Real-world advice for writing maintainable ... by solarkraft on Thursday February 21, 2019 @04:38PM (#19220782)
In short Go functions I tend to like single letter variable names. Maybe because it makes me closely consider the behavior of the program instead of trying to assume by the variable names.
Re:Practical Go: Real-world advice for writing maintainable ... by coldtea on Friday February 22, 2019 @12:35AM (#19223371)
Well, for most things ok, but i is so well established, than if you don't use it for loop indexes you you probably impede the readability of those reading your code
Re:Practical Go: Real-world advice for writing maintainable ... by eternalban on Thursday February 21, 2019 @06:36PM (#19221697)
I think 'k' and 'v' are fine for iterating over maps.
Re:Practical Go: Real-world advice for writing maintainable ... by pjmlp on Friday February 22, 2019 @01:20AM (#19223531)
I am fully there with you, the only exceptions are still using "I" in short for loops, e for WPF/Forms event handlers due to convention and typical math symbols like x,y,z.
Re:Practical Go: Real-world advice for writing maintainable ... by dnautics on Friday February 22, 2019 @01:43AM (#19223596)
I stopped using "i" when I had it in a context where it could be easily confused for "imaginary constant".
Re:Practical Go: Real-world advice for writing maintainable ... by PavlovsCat on Thursday February 21, 2019 @05:03PM (#19220952)
> I don't even use "i" for loop variables anymore.

Yeah, I noticed that for myself, too. so instead of i I might use frame_index, or whatever it "actually is". Up to a certain length it seems faster to just read what is there, without an additional mental translation step. But to be honest, I just do it because I like it.

Re:Practical Go: Real-world advice for writing maintainable ... by Gibbon1 on Thursday February 21, 2019 @06:21PM (#19221607)
I do that off and on, mostly because with the editor I use, it's harder to highlight single letter variables.