6 ms·
I'm still sitting here shocked that a language where "err" (and the keywords that check around it) are used an order of magnitude more frequently than all other
by pmarreck 9y ago
I'm still sitting here shocked that a language where "err" (and the keywords that check around it) are used an order of magnitude more frequently than all other syntax in that language, has achieved this much popularity: https://anvaka.github.io/common-words/#?lang=go https://anvaka.github.io/common-words/#?lang=go
- logingone 9y agoI've described Go programs as often looking like a listing of things that could go wrong.
- jalfresi 9y agoIn my opinion and experience, that is exactly what programs are
- bboozzoo 9y agoUnless you're writing in Erlang, in which case you program the success path and 'let it crash' in a controlled manner.
- grogenaut 9y agoYou're assuming that it faithfully crashes in all error states as opposed to incorrectly continuing on it's merry way.
- tsuraan 9y agoHave you used erlang? Pattern matching is a big part of it, and pattern match failures always result in errors. So, if you have some function foo() that always returns ok or {error, Reason}, then in the "follow the happy path" style, you'd just have a line reading "ok = foo(),", and then if foo actually returns something else, you get a "crash". There's no assuming anything there, it's just the way the language works.
- grogenaut 9y agoYes I have. but I see the erlang blindness still runs strong. Just because you have pattern matching doesn't mean you don't have bugs cause by humans. It can be syntactically correct and also logically not match the problem domain. For example what if you screw up and forget to encode that it SHOUDN'T return OK? you know that many other frameworks also have these safeguards right? Java server frameworks have been doing it forevever. You can code dirty if you want and deal with no issues or you can be error prone. Hell you can do the same thing in go. You can catch panics. It's not pretty but you can definitely do it. I forgot one can only point out strengths of erlang not problems with humans coding in general. all hail erlang.
- bboozzoo 9y agoLogical errors are always an issue, regardless of the language. As for Erlang's problems, it does have a fair number of warts and special cases, but the biggest of them all is that it's still a niche language. We actually had to dump Erlang for new projects in favor of Go. That may sound ridiculous, but filling positions involving Erlang was next to impossible (even with candidates without any prior knowledge of the language).
- pmarreck 9y agoWhy wouldn't it, if you're pattern-matching correctly? And in any event, it seems far easier to get into that situation in Go (which ignores errors unless you explicitly check for them) than in Erlang (which crashes on every error because restarting is cheap)
- grogenaut 9y agoMy point is... "Pattern matching correctly"... assumes no mistakes on the coder's part. Go back and re-read my comment. Or don't. Downvote me because I dare question your skill in the context of the mighty erlang.
- pmarreck 9y agoI would never downvote someone simply for disagreeing, and certainly not unless I was 100% sure they were wrong or it was way offtopic. (And you would be surprised how often I admit I was wrong or cede a point!) You're basically saying "well, it's still subject to logic errors." Well, that's a nonargument, because you can say that about 100% of languages in existence. That is not a criticism you can levy just against BEAM langs. And we were never arguing that BEAM magically takes care of your logic bugs. Just that it seems to handle the "set of all bugs that are not logic bugs" better. I also apologize for erlang/elixir fans being rabid and insufferable. ;) A lot of us have dealt with many other langs (I spent years on Ruby and other OO langs) and we're enjoying the unexpected (to us) benefits of this new/old world, that's all. Joe Armstrong is basically Einstein AFAIC...
- steveklabnik 9y agoYou can't downvote people who reply to you on Hacker News, so that scenario isn't possible.
- grogenaut 9y agoSure you can. You only need two accounts. Which a lot of people have for work posting or other reasons. I don't but I know folks who do.
- threeseed 9y agoNot in Java, Scala, Clojure etc. it isn't. You can wrap all your code in an unchecked exception and never again have to worry about errors. It's bad practice obviously but there are many situations in which you don't care what error occurred but that an error itself occurred.
- deleted 9y ago[deleted]
- mohamedattahri 9y agoWhich is exactly what software engineers should spend most of their time doing.
- wruza 9y ago<>, err = <> if err != nil { return nil, err } <>, err = <> if err != nil { return nil, err } Is not what software engineers should spend time on, definitely. Not automating ubiquitous trivial propagations with at least explicit "rie <expr>" (rie for "return if error") is a complete engineering fail under any philosophy. Oh, I have an idea. If ", err" part is missing from lvalue, insert that mantra automagically under #pragma ARIE=on. This workaround will give designers some stats to embrace.
- pmarreck 9y ago> Is not what software engineers should spend time on, definitely. But hey! Your editor can easily boilerplate all that... boilerplate! I think Go will continue to grow until we finally have metrics that say that coding something in Go might get you fast-running code but will be slower to code and hell to maintain.
- mohamedattahri 9y agoI consider that explicitly and manually handling every possible error, and making a conscious decision to bubble it up (return) or to interpret it is a very good use of my time as a programmer.
- thatswrong0 9y agoIt really doesn't have to be that way. See http://fsharpforfunandprofit.com/posts/recipe-part2 http://fsharpforfunandprofit.com/posts/recipe-part2
- slantedview 9y agoPython expats needed a place to go.
- geofft 9y agoI don't understand this comment - Python does what I'd argue to be the "right" thing, which is that operations which can fail raise exceptions that unwind the stack. If you don't care to handle failure in a particularly granular or graceful way (e.g., you're a command-line process, or you're a server process with lots of independent requests), you can have one large catch statement around all of your work, or even just use the implicit one around the entire program provided by the runtime. Meanwhile, well-written C code does this the "wrong" way; every external call has to be done like if (read(fd, buf, len) < 0) { hopefully remember to free some things; return NULL; } Why do you think explicit error handling is a Pythonic practice?
- spyspy 9y ago15 Million to 17 Million is not an order of magnitude.
- Svenskunganka 9y agoThe top 4 keywords in the list are what's used frequently in go to handle errors. if err != nil { return err } So it's more like 7,7 million to 17,6 million (the top fifth "the" is from comments, so it doesn't count)
- boomlinde 9y agoThat is not an order of magnitude either. Except in base 2 :)
- w8rbt 9y agoIf it weren't for Goroutines I would not find go very useful. I expect C++ will implement coroutines and/or fibers someday that will be very well designed and more carefully/broadly thought out.
- nly 9y agoWhy wait? Coroutines: http://www.boost.org/doc/libs/1_64_0/libs/coroutine2/doc/html/index.html http://www.boost.org/doc/libs/1_64_0/libs/coroutine2/doc/htm... Fibers: http://www.boost.org/doc/libs/1_64_0/libs/fiber/doc/html/fiber/overview.html http://www.boost.org/doc/libs/1_64_0/libs/fiber/doc/html/fib...
- w8rbt 9y agoGood point. I prefer ISO standards and the built-in library, but your point is valid. Implementations do exist in C++ today.
- osteele 9y agoThat's what kept me from looking at the language for the last five years. I finally broke down and wrote my first Go program. Yeah, the errors (and lack of generics) are annoying, but it gets enough else right – tooling, runtime speed, compilation speed, type inference, parametric polymorphism (on interface types) – that I enjoyed it anyway. Every language has some annoyances; it's nice when there are only a few.
- 43224gg252 9y agoNot only that, but you can just ignore errors during prototyping and with a little editor magic you can go back easily generate at least 85% of the if err != nil {... etc. statements. As much as people complain about it, explicitly checking errors like this (and checking them all) is almost essential for production quality enterprise code and any code running on mission critical systems (and to most project managers, all systems are mission critical). And fwiw I still prefer go's style of error checking to languages that implement massive try/catch blocks.
- pmarreck 9y agoI think the "embrace failure" supervisor model in BEAM is a superior design for mission-critical systems (such as cell networks, where it originated) while resulting in literally a ton less boilerplate. You just code the "happy path" and done. Any errors are logged and the process instantly restarted by the supervisor, you can follow up on them if they present a problem. If you were to code just this "happy path" in Go, it would actually be the unhappy path, because you'd have silent errors resulting in indeterminate state (read: horrible bugs to troubleshoot). If you believe programming is mostly about managing all possible state (as I do), then Go's strategy is way too explicit. In Erlang/Elixir/BEAM's case, as soon as state goes off the rails (read: encounters something the programmer didn't anticipate), it's logged and restarted by its supervisor process almost instantly. (and there are of course strategies for repeated restarts)
- XorNot 9y agoPeople keep saying this and I struggle to understand how this represents good design. In any language I can always just eat the error and keep going, or eat the error and restart. That doesn't fix the error, and it doesn't imply the program will work correctly.
- collinmanderson 9y agohah. It's #1 "err", #2 "if", #3 "return", #4 "nil". :) That must be because a lot of function calls are followed by: if err != nil { return err } https://stackoverflow.com/questions/18771569/avoid-checking-if-error-is-nil-repetition https://stackoverflow.com/questions/18771569/avoid-checking-...
- Thaxll 9y agoStill better that try / except: pass from other languages.
- barrkel 9y agoFrankly, I've never seen that (try / catch / ignore exception) in any commercial codebase I've worked on, running into tens of millions of lines. The worst I've seen is people logging the error message but not the exception itself (so losing the stack trace), or causing another exception to be thrown (and losing the stack trace or cause of the original).
- twblalock 9y agoNo, it's way worse, because you have to write the error handling multiple times even if it is all the same (and it usually is). For example, in Java, I can make as many method calls I want inside of a try block, and catch an exception from any one of them in the catch block. In Go, I would need the equivalent of one catch block per method call, in the form of "if err != nil { return err }". Why repeat yourself? If you want to pass on an error in Go, its easy to do accidentally -- just omit "if err != nil". Both types of exception handling models enable this kind of behavior. If you are going to do it, you might as well not repeat yourself while you are doing it.
- laumars 9y ago> Why repeat yourself? Because you can put more meaningful error messages due to the granularity of the checking. This has made debugging things a lot easier at times.
- twblalock 9y agoYou can, but you should not be forced to do so. I could easily do that in Java if I wanted to, but I also have the flexibility to handle them all in the same way, or pass them back to the caller (which the right thing to do 99% of the time), with a very small amount of code.