News for hackers. Stuff that matters.
Re:Practical Go: Real-world advice for writing maintainable ... by briatx on Thursday February 21, 2019 @02:21PM (#19219740)
> Naming the Config parameter config is redundant. We know its a Config, it says so right there.

> In this case consider conf or maybe c will do if the lifetime of the variable is short enough.

This seems petty. Is it really that problematic to type out a few extra characters?

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.
Re:Practical Go: Real-world advice for writing maintainable ... by networkimprov on Thursday February 21, 2019 @02:28PM (#19219791)
And this is worse advice:

> Functions should do one thing only. ... In addition to be easier to comprehend, smaller functions are easier to test in isolation, and now you’ve isolated the orthogonal code into its own function, its name may be all the documentation required.

Using single-caller functions as a substitute for comments makes the workings of a specific operation much harder to follow, as you have to jump around the source to understand its effects.

A long function is easier to understand than an exploded one.

Also tests should target specific operations (aka functional tests), not every single function in the program.

EDIT: Every function you add becomes part of your internal API. Any API, exported or not, should comprise a cohesive collection.

Re:Practical Go: Real-world advice for writing maintainable ... by ragona on Thursday February 21, 2019 @02:40PM (#19219868)
> A long function is easier to understand than an exploded one.

This is a pretty controversial position, and quite situational in my opinion. I absolutely agree that having to hop all over the source to understand something is frustrating, but that doesn't mean that very long functions are the right solution. Some combination of reasonably named helper methods and a function flow that makes the logic easy to parse should be the goal; either end of the spectrum is a problem.

Re:Practical Go: Real-world advice for writing maintainable ... by xtian on Thursday February 21, 2019 @03:29PM (#19220287)
Re:Practical Go: Real-world advice for writing maintainable ... by ra7 on Thursday February 21, 2019 @04:21PM (#19220672)
The summary is great:

> If a function is only called from a single place, consider inlining it.

> If a function is called from multiple places, see if it is possible to arrange for the work to be done in a single place, perhaps with flags, and inline that.

> If there are multiple versions of a function, consider making a single function with more, possibly defaulted, parameters.

> If the work is close to purely functional, with few references to global state, try to make it completely functional.

> Try to use const on both parameters and functions when the function really must be used in multiple places.

> Minimize control flow complexity and "area under ifs", favoring consistent execution paths and times over "optimally" avoiding unnecessary work.

Re:Practical Go: Real-world advice for writing maintainable ... by networkimprov on Thursday February 21, 2019 @02:49PM (#19219955)
I took issue with this because it's a conventional wisdom, and does a fair bit of damage.

Single-caller functions attract other callers over time, gain backwards-incompatible features, and result in regressions.

Re:Practical Go: Real-world advice for writing maintainable ... by Scramblejams on Thursday February 21, 2019 @03:09PM (#19220125)
This is one reason I like nested functions. They’re not available to the surrounding scope so they don’t succumb to these weaknesses, while also allowing you to organize your very long function internally by task. I use ‘em in Python all the time.

It’s a bummer that more languages don’t support them, though you can get there with lambdas too, sometimes at the cost of more syntax.

Re:Practical Go: Real-world advice for writing maintainable ... by networkimprov on Thursday February 21, 2019 @03:17PM (#19220207)
Go has closure functions; great feature.

One of the (few) things I like about Javascript is the ability to define a closure anywhere in the containing function, so it appears in the order of operations:

  function f() {
    setTimeout(fDing, 2000);
    g();
    function fDing() {
      ...
    }
  }
Re:Practical Go: Real-world advice for writing maintainable ... by johndubchak on Thursday February 21, 2019 @03:43PM (#19220408)
Disclaimer: I have no Golang programming experience.

I am curious with this approach though...you're nesting behavior and/or logic, doesn't this further obscure the meaning of the code and contribute more to the need to jump around the source in order to figure it out?

Re:Practical Go: Real-world advice for writing maintainable ... by vram22 on Thursday February 21, 2019 @05:56PM (#19221401)
Go has anonymous functions too.

The Go Programming Language book (Kernighan and Donovan) has some examples of them.

Re:Practical Go: Real-world advice for writing maintainable ... by SaltyBackendGuy on Thursday February 21, 2019 @05:39PM (#19221227)
Now write a unit test for

    function fDing() {
      ...
    }
Honestly with modern JS I am not sure this feature is that great. Looks more like a code smell these days imo.

Edit: Formatting

Re:Practical Go: Real-world advice for writing maintainable ... by Roboprog on Thursday February 21, 2019 @06:12PM (#19221543)
Now write a unit test for the inline block of code within the longer function which would have become the nested function???

You write the test (where any is needed) for the outer function.

Edit: the start of the discussion was about using nested functions to decompose what otherwise would have been “unpartitioned” long functions. Such blocks of code nested within a long function would not be possible to unit test, either.

Unit tests, rather than integration tests, are usually bogus, anyway, though.

Re:Practical Go: Real-world advice for writing maintainable ... by int_19h on Thursday February 21, 2019 @11:01PM (#19223018)
You write unit tests for units of code. A function with nested functions inside of it is a single unit of code; that's essentially what those functions being nested, and hence not directly invokable from the outside, indicates.
Re:Practical Go: Real-world advice for writing maintainable ... by kazagistar on Friday February 22, 2019 @10:52AM (#19226309)
Make private functions that are only visible to the current module. Then write your unit tests directly in that same module, next to the functions, so they can access them even when the rest of the world cannot. Of course, this requires sensible language support.
Re:Practical Go: Real-world advice for writing maintainable ... by Roboprog on Friday February 22, 2019 @09:39PM (#19231247)
Hierarchy vs list. I don’t want a list (of subroutines). I want a tree of self contained routines. Only your containing routine uses you. Which “private“ routines use which? (I know the IDE will tell me about this routine, and that routine, but I don’t want to have to ask)

Your employer doesn’t want you testing getters, anyway, but rather features. Unit test fanatics need to stop.

I guess unit tests were useful for C++, when it was constantly crashing everything :-(

C++ and its legacy need to ride off into the sunset, already.

Re:Practical Go: Real-world advice for writing maintainable ... by Roboprog on Thursday February 21, 2019 @06:07PM (#19221509)
A bit of our “heritage” in programming destroyed by the C family of programming languages

Pascal, like Algol, had nested subroutines for decomposing longer operations without leaking the details.

Nested functions is one of the things I like about JavaScript as well.

Re:Practical Go: Real-world advice for writing maintainable ... by Someone on Thursday February 21, 2019 @07:35PM (#19222051)
C has static functions for decomposinglo ger operations without leaking the details.

For better or for worse, taking advantage of that forces you to keep source files fairly short and cohesive in functionality.

Re:Practical Go: Real-world advice for writing maintainable ... by int_19h on Thursday February 21, 2019 @11:02PM (#19223022)
You are still leaking the details of the function that is the sole caller of those other functions. It's not leaking across translation unit boundary, but it's not the only one that matters.
Re:Practical Go: Real-world advice for writing maintainable ... by pjmlp on Friday February 22, 2019 @01:24AM (#19223547)
ML derived languages, Julia, D and C# support them.
Re:Practical Go: Real-world advice for writing maintainable ... by Gibbon1 on Thursday February 21, 2019 @07:22PM (#19221969)
GNU C supports nested functions as an extension. I use them for the exact reasons you mention.
Re:Practical Go: Real-world advice for writing maintainable ... by favorited on Thursday February 21, 2019 @08:48PM (#19222420)
That extension results in executable stack memory, which is going to be a non-starter for many developers.
Re:Practical Go: Real-world advice for writing maintainable ... by Gibbon1 on Thursday February 28, 2019 @04:10AM (#19270070)
I believe that that only happens if you pass a nested function as a function pointer. Then gcc emits a trampoline.

If you just call it from the enclosing function it's fine.

Re:Practical Go: Real-world advice for writing maintainable ... by ragona on Thursday February 21, 2019 @03:42PM (#19220400)
Yeah, agreed. I do this in Rust a lot too, and I picked it up while writing a lot of Python.
Re:Practical Go: Real-world advice for writing maintainable ... by tigershark on Thursday February 21, 2019 @10:35PM (#19222901)
C# has inner named functions.
Re:Practical Go: Real-world advice for writing maintainable ... by kazagistar on Friday February 22, 2019 @10:48AM (#19226286)
An excellent balance is to try to make a function only do one level of abstraction at a time. It's a bit of a flexible guideline, but basically you shouldn't call `isUserActive`, do some complex arithemtic, read extract data from a complex data structure, and call a templating engine in one function, since those a results all different levels.

As long as what are doing is approximately the same type of thing, it is fine to do a lot of things without breaking readability.

Re:Practical Go: Real-world advice for writing maintainable ... by zbobet2012 on Thursday February 21, 2019 @05:12PM (#19221015)
I believe you've misunderstood this. A function can be very long and do only one thing or very short and do many things.

The function:

  func ManyThing(i int) int {
   fmt.Println(i)
   return i+1
  }
does two things, and it's two lines long. The function tcp_send_message_locked (https://github.com/torvalds/linux/blob/master/net/ipv4/tcp.c...) does one thing at it's 261 lines long.

Shorter code is _indicative of_ orthogonality (what he asks you to break on)__but_ it is not the same thing.

Re:Practical Go: Real-world advice for writing maintainable ... by networkimprov on Thursday February 21, 2019 @05:41PM (#19221247)
My critique is of single-caller functions as a documentation device, not shorter functions.
Re:Practical Go: Real-world advice for writing maintainable ... by zbobet2012 on Thursday February 21, 2019 @07:14PM (#19221913)
But he doesn't advocate that... anywhere. He advocates factoring out orthogonal code to get shorter functions.
Re:Practical Go: Real-world advice for writing maintainable ... by bch on Thursday February 21, 2019 @02:42PM (#19219883)
Inclined to agree, expounded upon here (“Classes should be deep.”): http://alex-ii.github.io/notes/2018/10/07/philosophy_of_soft...
Re:Practical Go: Real-world advice for writing maintainable ... by entity345 on Thursday February 21, 2019 @02:48PM (#19219948)
> Functions should do one thing only

Well, that's extremely good, and standard, advice.

https://en.wikipedia.org/wiki/Single_responsibility_principl...

Re:Practical Go: Real-world advice for writing maintainable ... by jjuel on Thursday February 21, 2019 @05:06PM (#19220971)
Longer functions are much more prone to causing errors, and errors that are harder to find. It is honestly much better to have functions that do one thing and one thing only. Might not always be possible, but it is always the best way to code.
Re:Practical Go: Real-world advice for writing maintainable ... by networkimprov on Thursday February 21, 2019 @05:08PM (#19220987)
It seems that belief is unfounded. See the link posted by @xtian.
Re:Practical Go: Real-world advice for writing maintainable ... by happythought on Thursday February 21, 2019 @11:23PM (#19223110)
I think “doing one thing” and “getting called one time” are getting confused.

GetData()

FormatData()

UploadData()

Those could do one thing but could be called at several points within a larger program, which i think you are fine with. Or they could only be called once in which case i think you are saying it might make sense to just inline them.

If I understand you correctly, then I agree.

Re:Practical Go: Real-world advice for writing maintainable ... by PavlovsCat on Thursday February 21, 2019 @05:41PM (#19221252)
I don't necessarily agree with "one thing only". At least, for things that start simple and grow as needed, I split things into functions either to not have to copy and paste the same code, or for readability/structure purpose. But not out of principle and always, until I can't divide any further. I can still split things out into functions later, should I need it, but doing it "just in case" and then not even having a use for it doesn't save me time and just adds overhead.

Though it also depends on whether I'm doing something familiar or something new, if I'm doing something new I might split things up more to help me conceptualize the problem. But when I'm just making a quick CLI tool, I might put it all in main first and only split it up as needed.

Re:Practical Go: Real-world advice for writing maintainable ... by sythe2o0 on Thursday February 21, 2019 @02:27PM (#19219783)
The argument is that it is not fundamentally better to use the longer name in the given context, so why make it longer? I'd say it isn't about how long the variable takes to type either, but how long the code takes to read and parse.
Re:Practical Go: Real-world advice for writing maintainable ... by tdb7893 on Thursday February 21, 2019 @02:34PM (#19219833)
I'm wrestling with this at work right now and the short names don't really bother me as they are right in that the fact that if you have the type the shorter name can make the code easier to read slightly. What annoyed me is that for those single letter names I always got collisions so my naming was inconsistent. I personally ended up doing medium length names (like conf) except for the case where there was only 1 local variable (and sometimes if there were two or three) because in that case there were no collisions and what that variable was is very very clear
Re:Practical Go: Real-world advice for writing maintainable ... by sacado2 on Friday February 22, 2019 @04:57AM (#19224212)
It is problematic for readability reasons as long as you are making complex expressions:

    configuration := NewConfiguration()
    configuration[word] = parameters[values[line][column]] - parameters[values[column][line]]
vs

    conf := NewConfig()
    conf[w] = params[vals[i][j]] - params[vals[j][i]]
It takes your brain more time to parse the first line. Now, there is an obvious limit, this is probably too much

    c[w] = p[v[i][j]] - p[v[j][i]]
unless maybe the scope of the vars is very limited.
Re:Practical Go: Real-world advice for writing maintainable ... by dagw on Friday February 22, 2019 @06:33AM (#19224522)
In your example I would compromise with:

    conf[w] = params[vals[line][column]] - params[vals[column][line]]
or even:

    conf[w] = params[vals[l][c]] - params[vals[c][l]]
if you really want to save a couple of characters. Confusing which loop index is indexing which dimension is far too easy.
Re:Practical Go: Real-world advice for writing maintainable ... by Casperin on Friday February 22, 2019 @05:59AM (#19224427)
The first version was too long to fit on the screen for me. Reading the second I missed the swapping of `i` and `j` -- only noticing it when I went back to the first version to figure out which parts of line I would assign to something if I were to rewrite the code.
Re:Practical Go: Real-world advice for writing maintainable ... by ilovecaching on Thursday February 21, 2019 @06:54PM (#19221820)
It says so in the signature, but Go code tends to have long functions, so it's kind of a fallacy to say that it's going to be easily recognizable 50 lines in because it's in the signature. Now if this were Haskell and it was a single small expression, it would be different.
Re:Practical Go: Real-world advice for writing maintainable ... by akerl_ on Thursday February 21, 2019 @02:26PM (#19219776)
The issue w/ calling it "config" is that you end up with the confusing scenario where "config" is the object and "Config" is the type, differing only in casing.
Re:Practical Go: Real-world advice for writing maintainable ... by pcwalton on Thursday February 21, 2019 @02:48PM (#19219940)
Why is that confusing? Local variables are, idiomatically, never capitalized in Go, so the distinction is obvious at a glance.
Re:Practical Go: Real-world advice for writing maintainable ... by akerl_ on Thursday February 21, 2019 @04:05PM (#19220577)
Sorry. To clarify: it's not that I'm confused by the distinction between capitalized vs uncapitalized, it's that visually, "config" and "Config" look quite similar at a glance, whereas "c" and "Config" are clearly visually distinct.
Re:Practical Go: Real-world advice for writing maintainable ... by rat9988 on Thursday February 21, 2019 @02:53PM (#19219990)
As are unexported objects.
Re:Practical Go: Real-world advice for writing maintainable ... by ben0x539 on Thursday February 21, 2019 @02:55PM (#19220009)
Yeah, getting to idiomatically capitalize names has been pressure to move them to their own package in the past, which feels awkward.
Re:Practical Go: Real-world advice for writing maintainable ... by bpicolo on Thursday February 21, 2019 @09:16PM (#19222555)
This is a fairly go-specific issue due to its unexported vs exported convention. Many languages solve this through case conventions
Re:Practical Go: Real-world advice for writing maintainable ... by zerr on Friday February 22, 2019 @04:05AM (#19224020)
They don't use any IDEs or modern editors, but vim as a bare bones text entry, without syntax highlighting or word completion.
Re:Practical Go: Real-world advice for writing maintainable ... by karmakaze on Thursday February 21, 2019 @07:33PM (#19222039)
Also in this case Go makes you use types in the name since it doesn't allow function overloading.
Re:Practical Go: Real-world advice for writing maintainable ... by omeid2 on Thursday February 21, 2019 @07:44PM (#19222095)
It can be a problem with Config comes from package `config`.