I really like the horizontal 'Good/Bad' code comparisons in this guide. I didn't realize how horizontal vs. vertical code comparison affects readability; IMO horizontal is MUCH more readable. Example: https://github.com/uber-go/guide/blob/master/style.md#defer-...
+1. It is quite an achievement by Go as a language and/or community to have generally short lines of code where two columns fit in a regular website width. I don't like everything about Go, but it's usually pretty easy on the eyes for this reason.
Uber Go Style Guide
121–130 of 140 posts
Re: Uber Go Style Guide
#122"Copy Slices and Maps at Boundaries. Slices and maps contain pointers to the underlying data so be wary of scenarios when they need to be copied. Keep in mind that users can modify a map or slice you received as an argument if you store a reference to it. Similarly, be wary of user modifications to maps or slices exposing internal state." This could be used as an ad for Rust borrow checker, verbatim. You can't modify…
Cannot wait for "Uber Rust Style Guide".
Re: Uber Go Style Guide
#123Earlier quoted context omitted.
I'm sorry, but that is a naive misconception. "everything is owned by the GC" is a sign of not understanding concept of ownership, BTW. GC is not an actor in the system, so does not participate in the ownership. A Java finalizers might kind of do stuff, but that's when all references are gone, so they have exclusive ownership. Anyway... Languages with GC still do have ownership. It is just defaulting to be the most p…
Ok, then educate me—what is ownership if not the responsibility for managing (cleaning up) a resource? In my C and C++ days, I never heard of it refer to some kind of exclusive permission to mutate a resource (which seems to be your meaning), but rather as the responsibility to free the resource (as in “the owner must and only the owner _can_ free the owner resource”). It seems likely that you and I are using differe…
Re: Uber Go Style Guide
#124Earlier quoted context omitted.
Work on their internal chat app, I guess? https://eng.uber.com/uchat/
That's built on top of Mattermost which is open source. They do build their own infra instead of using cloud though so that probably uses a big part of it.
Re: Uber Go Style Guide
#125Earlier quoted context omitted.
Shared mutable state is evil, so "not mutable" has been proposed as a solution. Rust is different, because Rust's solution is "not sharing". You can still mutate!
I would describe Rust more like a read-write lock than not sharing.
Ownership and move semantics are checked at compile time, and allow effective "not sharing" without waiting or defensive copies.
Re: Uber Go Style Guide
#126Earlier quoted context omitted.
Ok, then educate me—what is ownership if not the responsibility for managing (cleaning up) a resource? In my C and C++ days, I never heard of it refer to some kind of exclusive permission to mutate a resource (which seems to be your meaning), but rather as the responsibility to free the resource (as in “the owner must and only the owner _can_ free the owner resource”). It seems likely that you and I are using differe…
In Rust, we also say that it’s the right to destroy.
Re: Uber Go Style Guide
#127I'll throw in one request for functional options as written here : please don't use closures to capture the data. If you use closures, it's nearly impossible to compare the results for equality in tests or code that might care to validate / deduplicate / whatever those args. You can achieve it with a moderate amount of heavily-implementation-dependent reflection (get the func type, construct arg(s), call func, check…
are you saying you’re against the functional options approach? can you provide an example of a testing difficulty you’ve had?
func MyOption(arg int) Option {
return myoption(func(opts type) { opts.thing = arg })
}
will result in this: MyOption(5) == MyOption(5) // false
Which makes it a pain in 1) testing, since you can't see the options that were passed, and 2) middleware, since you can't see the options that were passed, to validate or extend them safely. And 3) debuggers or Printf since you can't see the value in the closure until it's executed.Instead do:
type myOption int
func MyOption(arg int) Option {
return myOption(arg)
}
since it has none of those problems.Whether you use the "loop and opt.apply(arg)" pattern or not doesn't really matter - that's a purely internal detail (personally I find it over-simplifies things and causes problems). Just "avoid passing values through func closures".
Re: Uber Go Style Guide
#128This recommendation surprised me: if err := ioutil.WriteFile(name, data, 0644); err != nil { return err } I've often seen guides for many languages that say don't use an assignment as an "if" condition. It may be a typo, and so is a source of errors, or it hides errors. Many compilers warn about it, and some people will consider it poor enough to refactor it out. Of course it's not the assignment which is being teste…
It helps reduce uninitialised variables, and if the condition is false, you don't have an extra variable hanging around in the scope that could potentially be misused by the developer later.
Re: Uber Go Style Guide
#129Something that I don't see style guides addressing, but think they should, is how to cancel work when the caller no longer cares about the answer. (Consider an aborted RPC, or someone pressing the "stop" button in their browser.) A lot of people will do things like: func (s *Server) HandleFoo(ctx context.Context, in *input) status { ch This will block on the channel write even if the context becomes cancelled. Instea…
Re: Uber Go Style Guide
#130Earlier quoted context omitted.
I'm sorry, but that is a naive misconception. "everything is owned by the GC" is a sign of not understanding concept of ownership, BTW. GC is not an actor in the system, so does not participate in the ownership. A Java finalizers might kind of do stuff, but that's when all references are gone, so they have exclusive ownership. Anyway... Languages with GC still do have ownership. It is just defaulting to be the most p…
Ok, then educate me—what is ownership if not the responsibility for managing (cleaning up) a resource? In my C and C++ days, I never heard of it refer to some kind of exclusive permission to mutate a resource (which seems to be your meaning), but rather as the responsibility to free the resource (as in “the owner must and only the owner _can_ free the owner resource”). It seems likely that you and I are using differe…
I mostly write Java for a living, have done for a decade, and still occasionally trip myself up by mutating an object I'd forgotten I've shared.