4 ms·
You say "verbose and repetitive", I say "easy to read without any surprises". The verbose patterns (if err!= nil for example) make the code predictable to read
by theshrike79 5y ago
You say "verbose and repetitive", I say "easy to read without any surprises".
The verbose patterns (if err!= nil for example) make the code predictable to read, you notice the code smell of missing error handling really fast.
- coryrc 5y agoOr use Option or Result, so your error handling is small and forcibly correct.
- tptacek 5y agoError handling in Rust isn't always small or straightforward. I miss both options and match expressions when I switch back to Go from Rust, but there's also tangles of or_else's and maps and the fact that everyone uses third-party libraries to work out the types for errors. There's tradeoffs everywhere you look.
- int_19h 5y agoZig is probably a better comparison for this: https://ziglang.org/documentation/master/#Errors https://ziglang.org/documentation/master/#Errors
- takeda 5y agoI think readability has multiple dimensions, and it really depends what you are looking for. For example here's a code in Go to look for a Prime: func IsPrime(n int) bool { if n < 0 { n = -n } switch { case n < 2: return false default: for i := 2; i < n; i++ { if n%i == 0 { return false } } } return true } It's readable as it is simple to understand what each line does. Here for example is a code that does the same thing in Rust: fn is_prime(n: u64) -> bool { match n { 0...1 => false, _ => !(2..n).any(|d| n % d == 0), } } It's might seem more complex at first (what does match do, what 0...1 means, !(2..n) what is any() doing. But if you understand the language it actually this seem much simpler and you can quickly look at it and know exactly what it is doing. And because it is less verbose it is easier to grasp the bigger code. I also noticed that while individual functions in Go are simple to understand and follow, you can still create complex, hard to follow and understand programs in Go.
- bopbeepboop 5y agoYou changed function signatures from int->bool to uint->bool, which changes how long the functions are. That seems unfair when comparing: Removing negatives from the Go implementation removes 6 out of 16 lines, bring it from 3x Rust to 2x Rust in line length.
- takeda 5y agoI used code from: https://endler.dev/2017/go-vs-rust/ https://endler.dev/2017/go-vs-rust/ good point, I overlooked that. Anyway in Go (ironically because of lack of generics) if you use any numeric type other than int, int64, float64 you will be in the word of hurt. Rust doesn't have that issue. So in practice you will likely use int, and I suppose you can add an assertion. BTW: I only see that it would remove 3 lines though, where are the other 3?
- tptacek 5y agoI don't follow. I use unsigned ints in Go all the time. I've never been in a world of hurt with them. Mandatory explicit integer conversions (and the way Go consts work) are something Go gets right.
- takeda 5y agoOk, so you like that. I myself really hated when I used float32 and had to do this when I was doing calculations: result = (float32)Max((float64)a, (float64)b) I ended up switching the type to float64, and wonder why they even offer float32 if it's practically unusable. I had similar experience when I needed to use int8 or int16 etc. An alternative was to make own version of Max/Min and other math functions, but this is what generics would solve.
- tptacek 5y agoI'm not sure I follow, because Rust is also (thankfully) fussy about integer types.
- anothernewdude 5y agoYou're right that a templating language would reduce the code and make it easier to read.
- andy_ppp 5y agoPersonally I find code with generics just as easy to read as (largely duplicated) implementations for each type. I might consider Golang again when this drops and there are things to be like about how low level it is…
- the_gipsy 5y agoIn rust you have to swallow an error quite explicitly. In go it’s extremely easy to swallow one "err”, by assigning to some previous one which was already checked.