Most active commenters
  • gethly(4)

←back to thread

Go subtleties

(harrisoncramer.me)
234 points darccio | 25 comments | | HN request time: 0.001s | source | bottom
Show context
valzam ◴[] No.45666643[source]
Great list of why one can love and hate Go. I really did enjoy writing it but you never get the sense that you can be truly certain your code is robust because of subtle behaviour around nil.
replies(3): >>45666654 #>>45667227 #>>45671606 #
1. valzam ◴[] No.45666654[source]
I guess as a corollary, Go really rewards writing the dumbest code possible. No advanced type shenanigans, no overuse of interfaces, no complex composition of types. Then you will end up with a very fast, resource light system that just runs forever.
replies(5): >>45666706 #>>45666893 #>>45669853 #>>45671891 #>>45672663 #
2. theshrike79 ◴[] No.45666706[source]
And code with zero ability to do fancy trickery ("expressive" as some people like to say) is easy to read even if the codebase - or even the language - is unfamiliar.

Which is really handy when shit's on fire and you need to find the error yesterday. You can just follow what happens instead of trying to figure out the cool tricks the original programmer put in with their super-expressive language.

Yes, the bug is on line 42, but it does two dozen things on the single line...

replies(3): >>45667256 #>>45672153 #>>45673868 #
3. usrnm ◴[] No.45666893[source]
To be fair, checking if an interface is nil is very dumb code, and the fact that it doesn't work is one of my biggest gripes with the language. In this case it's clearly the language (creators) who's dumb
replies(1): >>45672472 #
4. spoiler ◴[] No.45667256[source]
I know it's not exclusive to Go or any language, but you can most certainly write incomprehensible code in it. If anything, expressiveness and proper abstractions can save you from this.

I think people often get burnt by bad abstractions in expressive languages, but it's not a problem of the language, but the author's unfamiliarity with the tools at their disposal.

If someone starts being clever with abstractions before understanding the fundamentals, it can lead to badly designed abstractions.

So I guess if there's less things to master, you can start designing good abstractions sooner.

So, in my experience, if we invest time to truly understand the tools at our disposal, expressive languages tend to be a great boon to comprehension and maintenance.

But yes, there's definitely been times early in my career where I abstracted before I understood, or had to deal with other bad abstractions

replies(3): >>45667769 #>>45669041 #>>45669329 #
5. DanielHB ◴[] No.45667769{3}[source]
I like to say this: "Only my code is allowed to be clever"

But, on a serious note, I agree with you. Go lacks a lot of power, especially in its type system, that causes a ton of problems (and downtime) that in other languages is trivial to prevent statically.

replies(2): >>45672342 #>>45673110 #
6. kace91 ◴[] No.45669041{3}[source]
>it's not a problem of the language, but the author's unfamiliarity with the tools at their disposal.

If you have to share a codebase with a large group of people with varying skill levels, limiting their ability to screw up can definitely be a feature, which a language can have or lack.

As always, it comes with tradeoffs. Would you rather have the ability to use good, expressive abstractions or remove the group’s ability to write bad ones? It probably depends on your situation and goals.

7. lagniappe ◴[] No.45669329{3}[source]
>most certainly write incomprehensible code in it

I've tried my best to make indecipherable go code and failed. Do you have any examples?

8. eweise ◴[] No.45669853[source]
You could say the same thing about any language. Writing "dumb" code is easier to understand especially in the small. But Go with function that take functions and return functions, channels and generics, can quickly look as complex as other languages.
9. h4ck_th3_pl4n3t ◴[] No.45671891[source]
Oh boi, all my RW mutexes for maps across goroutines would disagree.
replies(1): >>45672481 #
10. ErroneousBosh ◴[] No.45672153[source]
> And code with zero ability to do fancy trickery ("expressive" as some people like to say) is easy to read even if the codebase - or even the language - is unfamiliar.

A mate of mine did Comp Sci back in uni when First Years were taught Turbo Pascal showed me some, when I was still doing stuff in ZX Spectrum BASIC and Z80 assembler in high school. It was immediately clear what was going on, even if the syntax was a bit unfamiliar.

By contrast I've had to sit and pick apart things with strings and strings of ternary operators in the same expression, as part of case structures that relied on fallthrough, because someone wanted to show how clever they were.

My Pascal-using mate called stuff like that "Yngwie Malmsteen Programming". It's a phrase that's stuck with me, over 30 years later.

Don't do that "WEEDLYWEEDLYWEEDLY" shit. You're just showing off.

replies(1): >>45673514 #
11. ttz ◴[] No.45672342{4}[source]
out of curiosity (not meant snidely), do you have an example of a case where the weaker type system resulted in serious problems?
replies(1): >>45673305 #
12. gethly ◴[] No.45672472[source]
Interface is just behavior. That is the main difference from other languages. Go is about "what", not "who". So when you are checking for nil, you are essentially asking whether the variable has any logic it can perform. And that can happen only if some behavior was provided, ie. it is not nil.
13. gethly ◴[] No.45672481[source]
Use sync.Map or create simple wrapper to control access.
replies(1): >>45674040 #
14. gethly ◴[] No.45672663[source]
> Go really rewards writing the dumbest code possible

Simplicity is hard. You may see it as dumb, other see it as priceless attribute of the language.

15. 9rx ◴[] No.45673110{4}[source]
Formal proof languages are pretty neat, but nobody really uses them in the real world. Among the languages that people actually use on a normal basis, even those that claim to have extensive type systems, they still rely on testing for most everything, and once you're relying on testing anyway the type system isn't any kind of real saviour.

There is, perhaps, some segment of the developer community who believe that they are infallible and don't need to write tests, but then have the type system exclaim their preconceived notions are wrong, and then come to love the type system for steering them in a better direction, while still remaining oblivious to all the things the incomplete type system is unable to statically assert. But that's a rather bizarre place to be.

replies(1): >>45674447 #
16. DanielHB ◴[] No.45673305{5}[source]
Pretty much any null pointer deference error ever?

But it is hardly ever the weak type system that is at fault, just good use of a stronger type system could have prevented the issue.

Once you start to make "invalid states unpresentable" and enforcing those states at the edges of your type system suddenly a lot of bizarre errors don't happen anymore.

replies(1): >>45674948 #
17. theshrike79 ◴[] No.45673514{3}[source]
I always reiterate to junior programmers that you write as clever code as you want.

On your own time.

When you're writing code for work, stuff that other people have to eventually read and understand, you be as boring as possible. Skip all the tricks and make code readable, not cute. Someone might have to understand and fix it at 3 in the morning while everything is on fire.

  > Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
replies(1): >>45680027 #
18. cyberax ◴[] No.45673868[source]
Yeah, so pretty much every large project has concurrency bugs in Go. Because before generics, there was no way to write a parallel for-each loop without shooting yourself in the foot a couple of times.

Go is simple just like assembly is simple.

19. mook ◴[] No.45674040{3}[source]
… the documentation for sync.Map literally says:

> The Map type is specialized. Most code should use a plain Go map instead, with separate locking or coordination, for better type safety and to make it easier to maintain other invariants along with the map content.

The documentation basically says that it's optimized for some cases that wouldn't affect the complaint above.

replies(2): >>45674709 #>>45676715 #
20. tyre ◴[] No.45674447{5}[source]
Typescript’s type system is a huge leap up from JavaScript.

You still need tests for functionality (this function does what it should) but the type system removes many error cases automatically.

replies(1): >>45676595 #
21. gethly ◴[] No.45674709{4}[source]
If you want to be pedantic, you can use https://github.com/puzpuzpuz/xsync which has generics and is faster than native sync.Map
22. ttz ◴[] No.45674948{6}[source]
NPEs are also present in a lot of languages with "stronger" type systems though. Is there a specific language you're comparing against?
23. 9rx ◴[] No.45676595{6}[source]
It is a huge boost in developer ergonomics.

But doesn't change the tests you need to write, and those tests are going to incidentally cover anything the type system is also going to catch, so the type system isn't going to somehow make your software more reliable.

A much more expressive type system can get you there, but you won't find that in any language anyone actually uses on a normal basis.

24. h4ck_th3_pl4n3t ◴[] No.45676715{4}[source]
Well, sync.Map is based on atomics which comes with limitations on its own as you have to use specific (potentially conflicting) hash keys for it.

What I wanted to point towards with my earlier comment is that sync.Map doesn't use resource based mutexes, it uses one mutex for everything which will always be the slowest case.

There's no real borrowing concept in Go, as there would be in Rust for that case, and if you argue that simplicity should be preferred then normal map[] should be threadsafe by default, which, in return, likely will require compile time expansion because of generics.

The core value of Go's "we want simplicity" approach always feels like it's a facade, because there is so many exceptions and footguns along the way. These footguns almost always were conscious decisions where they could have made a decision to break legacy behavior for better simplicity but decided that this is not what they want, which is weird as it's not fitting the initial language design promise.

25. ErroneousBosh ◴[] No.45680027{4}[source]
I worked with a guy who hated comments. Twice a week or so he'd be "working from home" for some damn reason or another, and he'd spend the day removing all the comments from a massive and tangled PHP codebase. PHP4, at that, to give you a sense of how long ago.

Anyway his argument was "but the code should be obvious! You shouldn't need comments to explain what the code does!"

Yes Robert, but you need comments to explain what the code expects to do stuff to, and why you want that.

Turns out that removing the "Development Manager" as he styled himself's write access to the Subversion repository causes ripples in the fabric of reality right up to the C suite, but I could back my decision up with solid evidence that he was causing more problems than he was solving.