4 ms·
I don't know why "if err != nil" is such an issue, as C programs have been doing the exact same thing for decades and it works just fine.
by netr0ute 4y ago
I don't know why "if err != nil" is such an issue, as C programs have been doing the exact same thing for decades and it works just fine.
- Smaug123 4y agoDunno about "just fine" - I rarely use Go programs, but I found a bug in the one I work with the most which turned out to be precisely a failure to write "if err != nil". It just feels kind of embarrassing when we… already know how to prevent these problems.
- OkayPhysicist 4y agoThat's kinda Go's whole schtick: Google couldn't be assed to hire developers who had the baseline knowledge of their field, so they needed a language devoid of any of the useful advancements in language design made over the last 40 years except garbage collection. Its focus is to be written by the grossly inexperienced, and anyone who doesn't fit that category will miss basic functionality from every other language they've ever used.
- anyfoo 4y agoIf we could add Monads to C to stop having to do that, we would. This would require so much revamping of the type system that it wouldn’t be C anymore, in a non-compatible way. I’ve been writing C for at least 25 years, I am extremely comfortable with it and know most of its nooks and crannies, and I do it daily as my job. God I wish it had Monads just enough to stop cluttering my code with error checking boilerplate. There are many things in computing and everywhere else that we have done for decades, before we got far along enough to be able to stop.
- not2b 4y agoIt doesn't work fine if the check is omitted, which is easy to do, and if an error is extremely rare the missing check won't be noticed until much later. For rare errors exceptions are an alternative, but something has to catch and handle the exception.
- Jtsummers 4y ago"works just fine"? Like having a single global `errno` that you need to clear manually? Put this in a file `main.c` (or change the line opening `main.c` to a file known to exist): #include <errno.h> #include <stdio.h> #include <stdlib.h> int main() { FILE *fp; printf("%d\n", errno); fp = fopen("does-not-exist", "r"); printf("%d, %p\n", errno, fp); fp = fopen("main.c", "r"); printf("%d, %p\n", errno, fp); errno = 0; fp = fopen("main.c", "r"); printf("%d, %p\n", errno, fp); } The first `fopen` call sets `errno` to 2 (edit: really ENOENT, which happens to be 2). The second one should not have an error code but it's still 2 (ditto other edit). The third one demonstrates that the call does not, in fact, change `errno` since the file exists.
- deleted 4y ago[deleted]
- satvikpendem 4y agoThe point about all these languages that came after C that have exhaustive pattern matching on a Result type is that there are better ways of doing things. Why have the user have to handle cases like this by remembering to put in an `if err != nil` when the computer can literally compute where it's not being used and yell at you or even add it for you? Even in C it was not "just fine," it caused all sorts of bugs. But the bigger issue is that PL theory has improved since then, why be constrained to the same patterns as before?