<p><pre><code> if err == nil {
_, err := w.Write([]byte(g.Name))
if err == nil {
err := binary.Write(w, binary.LittleEndian, g.Age)
if err == nil {
return binary.Write(w, binary.LittleEndian, g.FurColor)
}
return err
}
return err
}
</code></pre>
Why does anyone have to tell people not to do this? How does it enter anyone's mind as a thing to do in the first place? I've been known to go <i>too far</i> to minimize nesting. I get twitchy at the second level. By the third my brain is trying to crawl out my eyes and strangle me, even on code that doesn't have a potential exit point at every level.
Odd choice of examples...<p>1. The file I/O makes the case for including exceptions in the language.
Specifically, adding one-off types to deal with exceptions is a bug, not a feature.
There is a good case against exceptions but that ain't it.<p>2. On slide 5, it appears to show that you have to use a switch statement on a generic
to get polymorphism because the language doesn't support overloading. Again, looks more
like a bug than a feature.<p>Also, is the "break;" implicit in Go? At first glance, it looks like a coding error.
Something that doesn't sit right with me is the use of a "channel of bool" when the receiving goroutine doesn't actually care whether true or false is sent. It muddies the API to force the sender to choose one of two values when all that's really wanted is an amorphous signal.<p>e.g. in <a href="http://talks.golang.org/2013/bestpractices.slide#25" rel="nofollow">http://talks.golang.org/2013/bestpractices.slide#25</a> , the first case in the select will trip regardless of which value arrives, yet the sender was still made to choose one.
The type cast as part of the switch is really cool, I hadn't seen that before.<p><pre><code> switch v := v.(type) {
case string:
w.Write(int32(len(v)))
w.Write([]byte(v))
default:
w.err = binary.Write(w.w, binary.LittleEndian, v)
}
</code></pre>
Great way to alter control flow based on the type, without a ton of ugly casts cluttering things up.
Meta comment: does anyone know what software is used to generate these slides? I've seen a few slide decks in the same format and they're impossible to use on mobile. I'd like to fix that
"Deploy one-off utility types for simpler code" can be called a monad or Optional. I wonder if the language developers will add more formal support for that; it looks impossible to add Optional as a library due to lack of user-configurable generics.
Thirteen: don't try to sort, it's going to be painful if you do.<p><a href="http://golang.org/pkg/sort/" rel="nofollow">http://golang.org/pkg/sort/</a> (See example 1 -- you have to write that for every concrete slice type you want to sort; it's not enough to write it once. And god help you if you also want to sort other collection types.)
If #6 is among best practices, I'm sad. I could take a dynamically-typed language instead.<p><a href="http://talks.golang.org/2013/bestpractices.slide#6" rel="nofollow">http://talks.golang.org/2013/bestpractices.slide#6</a>