134 points · by piinbinary · 5y ago · 150 comments
Have we learned nothing? Many languages without NIL/Nil/nil/null/NULL existed already when Golang was born.
And this attempt to heal the damage hurts, too:
> “make the zero value useful” philosophy
This is like instead of programming by exception (Java), it's programming by ignorance (of errors).
I like a few things about Golang (handling of numeric literals, for example), but not this thing.
In some respects, you can get around this by simply adding another field. You can have `bool over18; bool over18IsSet` and I'm guessing that Google's internal usage of protobufs does this.
In a certain way, even getting rid of null/default values doesn't fix all problems when it comes to things like updating data. Think about updating a record where a field could be set or unset - let's say a person's age could be a number or empty. If I want to send a request updating their name, maybe I send `{"name": "Brian"}` because I don't want to update the other fields. How do I unset Brian's age? `{"age": null}` makes some sense, but a Java (and many other language) deserializer will have null for age with `{"name": "Brian"}` too. I mean, the age field has to be set to something in the Java object. You could manually read the JSON, but that's janky and brittle - and hard in terms of interoperability with libraries and languages.
Maybe Google's protobuf designers would argue, "you really need to have explicitness around your values and forcing defaults means forcing engineers to deal with things explicitly."
I don't think I agree with that. I don't like Go's nulls and default values. I think most languages are moving away from that kind of behavior with other new languages like Kotlin and Rust going against it and older languages like C#, Java, and Dart trying to bolt on some null-safety (Java via the `Optional` object and C# and Dart via the opt-in `TypeName?` similar to Kotlin). It's possible that this is a wrong direction chosen by many languages. We've seen bad programming language fads before. In this case, I think we're on the right track and Go's on the less-great side.
Go has a lot to like, but this is one of those odd decisions. I understand why they did it. Go comes from Google where Protocol Buffers have similar default-value behavior. I think Go would be better if it had made some different decisions in this area.
If you don't have a zero value, a programmer has to pick one. What are they going to pick? Probably what the language picks for you, "int n = 0;", 'string foo = "";', etc. For a language, it doesn't really matter which side you pick (force programmers to select a value, or auto-assign one). For network protocols, defining empty is an important optimization -- if the client and the server are guaranteed to agree, you don't have to send the value. This is especially important where the client and the server aren't released at exactly the same time; the server may have a new field in the Request type that the client doesn't fill in. With a predefined zero value, it doesn't matter. (You can always add fields to your message to get the same effect, if you actually care. I've never seen anyone do this in any API, including ones that use serialization that doesn't have the concept of zero values. It's why Javascript has the ?. operator!)
Finally, Go came out in the proto2 era, which did have the concept of set and unset fields (and let the proto file declare arbitrary default values). Honestly, I wrote a ton of Go involving protos at Google, and never saw proto3 until after I left Google.
Most languages without null have an optional type which is used exactly for that. In these languages, this means the None value exists when you have optional things, but that, then, the compiler forces you to check whether your value is set when you want to use it. Serialization libraries then get the choice to handle these optional values as they wish, which can be not to send a field.
It's one of those things that may be hard to think about from an external pov, but it works just fine.
From users of these languages, proto2 was okayish, and proto3 was a massive regression. Another thing that's missing in protobuf is the ability to define union types. That's one frequently asked feature from typed functional languages for serialization protocols.
Even (modern) C/C++ handles this aspect of memory safety better: There is no default value, and reading from an uninitialized value is a compile time error (usually, because C/C++ have baggage).
This was one of the significant changes from proto2 to proto3.
This was also met with much opposition internally, and recently changed as of v3.12 (released last year).
https://github.com/protocolbuffers/protobuf/blob/master/docs...
https://github.com/protocolbuffers/protobuf/releases/tag/v3....
In my horrible opinion, we shouldn’t ditch null. We must introduce null flavors (subclasses) instead and fix our formats to support these. One null for no value, one for not yet initialized, one null for unset, one for delete, one for type’s own empty value, one for non-single aggregation (think of selecting few rows in a table and a detail field that shows either a common value, or a “<multiple values>” stub - this is it), one for SQL NULL, one for a pointer, one for non-applicable, similar to SQL. Oh, and one for not-there-yet, for async-await (a Promise in modern terms). These nulls should be enough for everyone, but we may standardize few more with time. Seriously, we have three code paths: normal, erroneous and asynchronous. Why not have a hierarchy of values for each?
Semantically all nulls must be equal to just “null” but not instanceof null(<other_flavor>).
Edit: thinking some more, I would add null for intentionally unspecified by data holder (like I don’t share my number, period), null for no access rights or more generic null for “will not fetch it in this case”. Like http error codes, but for data fields.
Here is different proposal. Let's allow people to define their own types of missing values. We'll call it Nullable<T> or Maybe<U>.
Such a system can be done, but it is probably super ugly and confusing. The usual answer instead is: exceptions, i.e., instead of instanciating an error object, throw an exception (well: you do instanciate an error object here...). That works, but if overdone, you get programming my exception, e.g., when normal error conditions (like 'cannot open file') are mapped to exceptions instead of return values.
The usual answer to that problem then is to use a special generic error class that you specialise for your type, the simplest of which is 'Optional' from which you can derive 'Optional<MyType>'. You can define your own generic type 'Error<MyType>', with info about the error, of course. I think (please correct me if I am wrong), this is currently the state of the art of doing error handling. It's where Rust and Haskell and many other languages are. I've seen nothing more elegant so far -- and it is an ugly problem.
[0] https://doc.rust-lang.org/std/result/ https://caml.inria.fr/pub/docs/manual-ocaml/libref/Result.ht... https://hackage.haskell.org/package/base-4.15.0.0/docs/Data-...
The way this is addressed in Google's public APIs is with the use of a "field mask"[1]. You provide a list of dotted paths to the fields you want to update. I'm not sure if that serves as an indictment of the design decisions made in protobuf, or if it's just one less bad tradeoff among several bad ones.
[1]: https://github.com/protocolbuffers/protobuf/blob/e9360dfa53f...
To me this looks like a discussion about syntax and ergonomics. Go provides the same mechanisms:
* for safe dereferencing, use "val, ok := *valRef"
* for potentially-unsafe dereferencing, use "val := *valRef"
Every language in that list has equivalent mechanisms. In Rust you can use one of these methods: https://doc.rust-lang.org/std/option/enum.Option.html or pattern matching. That's a whole lot to pick from.
But Rust also makes it more complicated to reason about your memory, see for example https://doc.rust-lang.org/std/option/#representation
So, given Go's design tenets, using pointers makes a lot of sense to me. It is easy to reason about them in terms of memory and resource consumption, there are only a few ways they can be used, pass-by-value semantics further reduces the centrality of them, and they don't require a JIT compiler to be efficient.
Edit: Nevermind, I forgot this is only of T is a reference. Otherwise it's layed out like a normal enum.
Interesting side-point re. language comparisons I noticed recently -- Java is often benchmarked together with compiled languages, although I would say it's only half-compiled (to bytecode, not to machine code). That's
But yeah: I once heard Go described as "a C fan-fiction". It was designed by and for people who use and like C, and just wanted some niceties built on top of it for their usecase of writing distributed systems (garbage collection, strings, easy networking and multithreading, easy cross-platform builds, very basic polymorphism). It was not designed by people who were interested in stepping back and re-evaluating the big picture beyond those practical necessities
I see it as C++ (literally, "C improved"), for the modern world (instead of the 80s). For all the good and bad that implies.
It's more of an alt-java than a C fan-fiction.
Most of the stuff people are interested in in C, Go dropped. However while Go clearly disagreed with Java on the specific means and most important bits the core goals and aesthetics are extremely strongly reminiscent of Java's.
That's a consequence of the "always mutable" model whereby the responsibility of initialization can be shared by the object constructor and the caller.
But there are many cases where it's very intuitive for the caller to set up an object, especially in the objects as actors model.
I think to make that work, you'd want to track the evolution of the object's state, especially noting when all fields have defined values.
I've seen all these approaches in Rust.
It's not though. An "always mutable" model can require that objects be fully initialised up-front. Rust does exactly that.
The reality is that you don't know where your nil-dereference bugs exist because your language doesn't detect and force you to eliminate them.
Which means you can check after a few years how many nil-pointer exceptions you got in your actual system. In my experience that numbers is really low.
Null made since in the early days of C, an option type back then would have been prohibitively expensive. Today, not so, any language that can afford garbage collection, the cost of an option type is lost in the noise, and modern compilers can often optimize the cost away entirely. (though that would hurt Go's fast compile times a little bit)
Although I would agree it was possible to design the language much better to avoid Null, most buffer overflows etc.., C was not designed to be good from PL standpoint. It was designed to write something and then evolved.
Of course not. Option types are as old as C, and they don't cost anything more at runtime than a regular pointer, or a union tag, costs.
I suppose not, if you are considering "we" as "the set of people that designed languages like Go or justify the design decisions made by such group".
If you are considering "we" as humanity, in general, I'd say that no, "we" actually learned a lot and "we" already know better. But you still have some building rockets to try to prove the earth is flat, unfortunately.
And I'd say it fails at that goal, judging by the number of gotchas in fundamental concepts of the language like slices or interfaces.
A surprising behavior of slices that had discussions on HN, and something I discovered in my own code as well: https://news.ycombinator.com/item?id=9555472
And of course this gem was on HN just yesterday: https://news.ycombinator.com/item?id=26631116
This was never a goal of go. Go was conceived out of frustration with c++. They wanted to reduce language complexity and build times (among other things). For me personally it's C without the hassle.
My harebrained idea (obviously we can't change it) is that if nil pointers didn't exist the language would be much better. Require that a pointer always has to point to an address and then people couldn't have built all this infrastructure using nil pointers (something that rightly has to do with reference semantics) to indicate nil values (something that has to do with the meaning of the code)
package main
import (
"fmt"
)
func main() {
foo := NewFoo()
foo.sayHello()
}
type Foo struct {
}
func NewFoo() *Foo {
return nil
}
func (f *Foo) sayHello() {
fmt.Println("Hello, World")
}
In Go, the function to be called by the Expression.Name() syntax is entirely determined by the type of Expression and not by the particular run-time value of that expression, including nil. user, err := users.FindByID(id)
if err != nil {
return nil, err
}
If the return value is just a pointer, in my books a nil value should be avoided. I guess what's not cool here is that there are no guarantees. If I'm using a library in which a function returns just a pointer, I sometimes jump in the function's body to check if it ever returns nil so that I don't have to pollute my code with `if` statements. Like, you almost never check for `nil` when dealing with simple factories, eg foo := NewFoo(). However, when it comes to a more complex method calls, it might be safer to add a quick `nil` check and forget about it.Only if you come from a vtable-based OO language. Go isn't one, so while it looks unconventional, it is rational.
x = A().B().C().D().E().F()
for example. Very useful when dealing with big complex trees.
It's fine to call methods on a concrete nil. But...
> that’s part of the “make the zero value useful” philosophy.
... the zero value of an interface is not a concrete nil! And if the interface does contain a concrete nil, it's no longer nil itself!
If the goal was to make zero values useful, Go should have rather have offered Obj C-style messages to truly nil interfaces - nothing happens and all values returned are zero.
IMO this is the worst thing about Obj C. The number of times I’ve discovered code that was silently failing in some Obj C project.....
You're forced to act on the little box that may or may not be empty, and make explicit what to do in both cases.
For someone like me it's wonderfully obvious and easy to understand and make a mental model of.
Elm if you favor "absolute simplicity", it's a pure functional language which eschews all the advanced type stuff found in most functional language. And I do mean all: elm actually eschews abstract types entirely, it doesn't have traits, interfaces, … except for a limited number of built-ins (comparable, number, appendable, and that's about it, well there's also compappendable which means comparable & appendable, and some people advocate for removing even those: https://discourse.elm-lang.org/t/just-for-fun-what-if-there-...).
It used to have a concept of "extensible records" but I'm not sure even that remains.
>Here we start to see some sources of real bugs. Writing to and reading from a nil channel blocks forever.
Of course they block forever. If you read from a channel that nothing is writing to it, you block forever. If you write to a channel that nothing reads from it, you block forever. There's nothing special related to the channel being nil.
Trying to assigning to a nil map is also perfectly understandable: a nil map is read only. This is the same as nil slices. "But `a = append(a, 1)` works when a is nil", you might be asking. Yes. That's because `append(a, 1)` is not (always) writing to a.
Then it talks about typed nils. Yes typed nils can be a footgun, but I've been writing go code professionally for almost 5 years now and I've been bitten by that exactly once (I've written about it here: https://wang.yuxuan.org/blog/item/2020/09/typed-nil-in-go). In most cases, typed nils are more useful than being a footgun. See https://pkg.go.dev/github.com/reddit/baseplate.go/log#Wrappe... and https://pkg.go.dev/github.com/apache/thrift/lib/go/thrift#TC... for a few examples of typed nils are being useful and providing useful zero values.
Internalizing this understanding was a challenge worth mastering, for my own work. What helped was to make the abstractions concrete in a running program, with the help of Delve's "examine memory" CLI command. Similarly for slices and maps.
Does Golang need Options and Results? Yes.
Have any experienced Go devs found cases where this behavior is useful?
select {
case foo := <-whateverCh:
log.Printf("hey a foo: %v", foo)
case <-ctx.Done():
log.Println("the user doesn't care about the answer anymore, so let's not leak the goroutine")
}In practice, in cases where a nil read/write could happen, a default "fall through" option on a select statement is used.
They could have made the original interface return a possible error for reading/writing, in line with regular go error handling, but opted instead to use more channel-based conventions.
Honestly, I see the ability to block indefinitely as a bug in the language. An improved language would probably "result, err := <(ctx)- ch", i.e. make it impossible to block without something bounding the duration of the block. (Also kill sync.Mutex. 100% of Go concurrency bugs are from people mixing mutexes and channels.)
(As an alternative, the runtime could be smarter about goroutines that have blocked forever. I am not sure exactly what it would look like, but the hypothetical first Go program I talk about above would probably be saved by something that killed goroutines that were waiting for a message from a TCP connection that is long gone. I think if it were easy to do right, it would have been done, though.)
...Get over it?
Yet you probably know more Latin from programming than you think you do. Did you know that integer is Latin?
> It's a dead language.
"In fields as varied as mathematics, physics, astronomy, medicine, pharmacy, biology, and philosophy Latin still provides internationally accepted names of concepts, forces, objects, and organisms in the natural world."
This list is by no means exhaustive.