4 ms·
> Because having any language facilities for error handling is harmful. No, making error handling verbose mandatory is meant to make developers do mental cardi
by bayindirh 3mo ago
> Because having any language facilities for error handling is harmful.
No, making error handling verbose mandatory is meant to make developers do mental cardio and be mindful of what they are doing.
You can either keep developers mindful or punish them after they make a mistake by shouting at them and make them waste their time during re-compiling.
P.S.: Yes, I love Go's error handling mechanics, which makes handling errors mandatory, not optional, and if you ignore the error, this is a deliberate choice and the burden is on the developer.
Go does the former, Rust does the latter.
- sapiogram 3mo agoWhat are you talking about, only Rust actually forces you to handle errors. Go functions merely return a tuple with an error along with the result, with a convention that you must checktror a non-nil error before using the result. Rust bakes this into the type system, a function can truly return a result or an error.
- bayindirh 3mo ago> with a convention that you must checktror a non-nil error before using the result. So, you handle the error in the end, or forcefully and intentionally ignore it. Again, if the code goes boom, it's on the developer, not on Go. > Rust bakes this into the type system, a function can truly return a result or an error. Error being a variable or baked into the type system doesn't change the practical result. You must handle the error or purposefully ignore it. > only Rust actually forces you to handle errors. When you have two programming languages which makes you handle the error, the word only becomes a little invalid. Semantics doesn't change the result. You have to acknowledge and act on the error either way.
- resonious 3mo ago> if the code goes boom, it's on the developer This is the same argument C (and Zig!) people have for manual memory management. You can avoid memory problems by being a good developer.
- yoghurtwere 3mo agohttps://www.reddit.com/r/programming/comments/1p0srgs/comment/npld8vh/ https://www.reddit.com/r/programming/comments/1p0srgs/commen...
- stefr- 3mo agoYou pasting this comment link on all the replies clearly shows you don't understand what actually went wrong in this Cloudflare outage. But Rust bad.
- bayindirh 3mo agoWhile I have my opinions on manual memory management, I'll not open that file today (well, it may leak). On the other hand, Go explicitly warns and tries to prevent you from ignoring or not handling possible errors. This is a bit different than a happy C compiler which doesn't warn you about leaking memory.
- madeofpalk 3mo ago> So, you handle the error in the end, or forcefully and intentionally ignore it. Again, if the code goes boom, it's on the developer, not on Go. Except Go doesn’t actually require you to handle the error. You can forget to handle the error, or forget to do a nil check. And Go won’t tell you until it crashes and explodes at runtime. Technically the user’s fault, but good systems protect the users from their own mistakes.
- majormajor 3mo agoIt's not hard to ignore an error in Rust, IME? Especially as someone who's read a lot of code written by newcomers to Rust.
- yoghurtwere 3mo ago.unwrap() https://www.reddit.com/r/programming/comments/1p0srgs/comment/npld8vh/ https://www.reddit.com/r/programming/comments/1p0srgs/commen...
- rowanG077 3mo agoUnwrap is an explicit action. Checking an error is usually just forgotten. No unwrap can enter your code without the dev being accutely aware of it.
- bayindirh 3mo agoYour Go code won't compile if you don't check the error, though.
- rowanG077 3mo agoDid you ever use go? ``` package main import ( "errors" "fmt" ) func getMessage() (string, error) { return "DO NOT INSPECT", errors.New("something went wrong") } func main() { msg, _ := getMessage() // Ignore the error. fmt.Println(msg) } ``` Compiles normally and prints "DO NOT INSPECT"
- tomhp 3mo agoRust is neither the only nor the first language to contain a result type (https://en.wikipedia.org/wiki/Result_type https://en.wikipedia.org/wiki/Result_type). Definitely a much nicer way to handle errors though.
- wannabe44 3mo agoIn practice you use Go with a linter, so this is not a problem in practice. I have never seen a missed error. The problem is painstakingly and manually having to build the stack trace, and then unwrapping it few layers above if you ever need to check what error it was.
- bayindirh 3mo agoYou don't need a linter. Practically Go code will not compile without declaring and using the "err" returning from a function call. I have written a fresh example at https://files.bayindirh.io/misc/error_example.go https://files.bayindirh.io/misc/error_example.go. Give it a Go. ;) OTOH, gopls is a great LSP, though, and it warns you about this immediately.
- comex 3mo agoThe problem is that this stops working once you have multiple assignments to err in the same function.
- bayindirh 3mo agoGood point, but I personally never do that? Instead of doing (expensive) calls, I just assign the result to a variable and use that instead. Accessing to memory is pretty cheap in Go, and it's cleaned once it goes out of the scope, so why not? Considering some expensive Go calls do the same caching underneath, this is the preferred design pattern, I assume.
- deleted 3mo ago[deleted]
- wannabe44 3mo agoWhat if the method only returns an error, like file.Close()?
- yoghurtwere 3mo ago.unwrap(). I am skeptical about Go's error handling, but there are cases where it is desirable to return both a result and also an error, like returning what can be returned while warning users about any errors. That can be modeled in Rust and other languages as well, though it is the default for Go.
- KronisLV 3mo ago> which makes handling errors mandatory Unpopular take, but I personally also believe that unchecked exceptions in Java and other languages like that are an issue - because it makes too easy to miss where things could go wrong, instead of making you visibly handle the potential known failure points, or indicate to callers what you're not handling.
- JyB 3mo agoThat's actually very popular with pretty much anyone who's dealt with errors happening in programs... which I would assume is most devs after some time.