185 points · by signa11 · 5y ago · 90 comments
The fact that c++ has changed so much in the past 10 years - it’s possible to write completely type and resource safe code in c++ now - this passed me by until a year or two ago when i attended a Bjarne talk.
Also the c++ folks have written up a core guidelines doc (this was the main topic of that Bjarne talk), and it’s really good https://github.com/isocpp/CppCoreGuidelines/blob/master/CppC...
"It's possible" is a rather low bar. It's also "possible" in C. Is it easy though? I wouldn't say so. Not if you push lambdas over queues to be invoked in another thread, or in the same thread, but later. Not if you take references to elements of std::vector. Not if you use std::vector<bool>. Not if you use operator[] on std::vector/std::array without tweaking some macros, because C++ committee in 2021 is still not convinced that bounds-checking is a reasonable default.
It's very easy on the other hand to write perfectly idiomatic "modern C++" which is memory-unsafe. Well... Easy if you figure out which one, among a dozen, is the right way™ to initialize a variable. (And then go and read code which initializes std::vector in all those different ways and try to guess which one does what.)
I also really enjoy listening to the CppCast podcast by Rob Irving and Jason Turner, it's a nice way to follow what is happening with the language: https://cppcast.com/.
I recently wondered where one could find new C++ software since there's no announce list at all.
[1] Alexander Stepanov, of STL fame, hated everything OO and called it "gook". http://www.stlport.org/resources/StepanovUSA.html
Some of those languages that nobody uses, are really good, though.
Programmers that learn them hit well above their weight-class in individual-productivity, and if they're lucky they get to support applications written in "dead" languages for fat stacks. Oh and they're often fun too -- who doesn't want to have fun for the 1/3rd of their life that we're supposed to be working?
I think I understand that some people want to be popular more than they want to be good, but what I find really weird is the people who would rather be close to popular instead of close to good. What's the point?
But of course, having fun is really important.
Such as?
No, not really. I filter for Go, Rust, Nim, and Dart on HN. Rust is the most popular with at least a post every day or two and Go about half that time or less. There used to be way more Go articles in past years. Some of the recent Go posts have gotten traction so maybe you're seeing that.
> no not really
This doesn’t make sense to me at all. 50% as popular as Rust is considerably fashionable. I imagine that Rust is one of the most common programming languages to appear in post titles on HN.
All this changed when I started learning Rust, man was I blown away by the language.. I never thought a statically typed language with manual memory management can feel so "scripting language" like.(Sure the borrow checker annoys you sometimes, nothing a `.clone()` can't fix when you are starting out). I truly enjoy writing Rust these days and never have I missed anything major from Ruby.
I'll be damned if I sit here and criticize Ken Thompson and Rob Pike, but I feel like in their quest to make Go "simple" they've perhaps held a little too hard on to their past.
tldr; Go feels a little too verbose and "dated" after learning rust.
Currently love Rust, but Crystal, Nim, and Swift are all rather distracting.
Once Go gets generics I’ll be interested in giving it a second look.
https://rytisbiel.com/2021/03/06/darker-corners-of-go/#mapst...
Honestly? Not everyone wants (or should have) to do that in order to touch some Go code (or code in any other language). I for one am quite happy that "cheat sheets" like this one exist - it might sound paradoxical but I'm not interested in actually learning every single language I've had to tinker with, as I have plenty of other things I'd rather be doing.
For hobby projects maybe, if it's part of your job you should know what you're doing and read the fine manual (basic documentation in this case).
> IMO, Java, even with a longer standard, is a safer and more sane language and ecosystem to use.
There's an entire book titled "Java Puzzlers".
And the extremely leaky implementation detail of maps and slices is definitely a gotcha. Usage wise both slice and map can be nil, but map has to be make()d, for no obvious reason.
The fact that the author was confused about "strings can't be nil" shows their Java experience, I think. Coming from C++ programmer I would go "well of course it can't be nil". (But slices and maps can? Wat?)
Slices have another gotcha, described, where yes there are arrays, but you basically never use them. They're just a ghost that pops its head in and is the explanation for why your code that treated slices like lists/arrays failed.
You're (almost) only supposed to use this leaky abstraction that'll bite you if you think your "list of items" is "a list".
I say this as someone who's coded a lot of Go, and reviewed much much more: Go, like other things Rob Pike makes, only make sense and are consistent and intuitive inside his brain. They don't make objective sense.
The poster child for this is "Go doesn't have exceptions". It has "unwind the stack, call cleanup code along the way, until a handler is met. If there's no handler it'll terminate the application". In Rob Pike's brain the very definition of "exception" is something completely different from "exception".
Even the defense "we don't call them exceptions because they should not be used e.g. for failing to open a file!" is complete nonsense to anyone who's ever heard of C++. Yeah. File not found is not an exception in C++, what's your point?
That and "well OF COURSE the go compiler for ARM is called 5g and x86 is called 6g!!!".
x = realloc(x, ...)I didn’t know this about go specifically (as I generally expect maps to be unordered). But it is pretty funny because the Python folks did the opposite: they ensured order stability on the dict because people started relying on it.
Another interesting but underused capability are go plugins, which allow us to dynamically load external code at runtime. They are similar to DLLs but unfortunately not as powerful as they e.g. need to be compiled against the exact same Go version & runtime. We still use them at various points in our codebase (e.g. here: https://github.com/kiprotect/kodex/tree/master/plugins/write...).
Hence why they are underused.
It's just that the reflection is a powerful tool AND a necessary evil.
https://blog.golang.org/laws-of-reflection
"Once you understand these laws reflection in Go becomes much easier to use, although it remains subtle. It's a powerful tool that should be used with care and avoided unless strictly necessary."
Note that this implies you need to avoid any operations that might resize the slices.
I'm reading this and I can't shake the feeling that this is really bad compiler and/or language design.
Why would the compiler import (that is, parse and compile) a package that's unused in the code?
> A better solution is to use goimports tool. Goimports removes unreferenced imports for you. What’s even better it tries to automatically find and add missing ones.
Why can't the compiler do that? Why do you need a few dozen separate tools to complement the compiler?
Parse - yes, compile - no. Compiler still needs to parse your code to determine if your import is used or not.
Go team decided that compilation should take as little time as possible, so it is easier to make sure that the compiler does not have to worry about unused imports at all.
Not to mention that this keeps your code clean.
Dozen separate tools can be used effectively in your workflow (say CI\CD pipeline). Or just executed on every save. (or both)
Parsing is easy. And it's fast. On top of that the compiler already knows it's unused:
--- start quote ---
Go programs with unused imports do not compile. This is a deliberate feature of the language as importing packages slow down the compiler.
--- end quote ---
So, the compiler has already parsed the file, and determined that the import is unused. And refuses to compile further, because... it needs to import that package anyway? Why?
> Dozen separate tools can be used effectively in your workflow
Haha, no. If you need a dozen different tools, there's no question about effectiveness at all.
Because it may have side-effects, and the compiler doesn't know if it's the programmer's intention to import in order to get those side-effects, or if it's a mistake.
I'm sorry to hurt your feelings, but compiler definitely can know whether some external module is used in the file it's compiling now.
See my other comment: https://news.ycombinator.com/item?id=26553401
Without opening more files and parsing them, how does it know that none of the transitive dependency tree has a `func init(){}` ?
Go forces you to add an underscore if you mean "I'm importing this for its side-effects, and am not using it directly".
"Opening bracket must be placed at the end of the line in Go. Interestingly this is not enforced by gofmt,"
Eh, no, of course it's not. It's a syntax error, fixing those are NOT the point of go fmtThis is a Light Corner and a Dark Corner of C.