News for hackers. Stuff that matters.
Re:Warming up to Go by bluejekyll on Saturday September 12, 2015 @10:50AM (#10208226)
But the expense of extra lines of coke come at significant maintenance costs. It means needing more test and the inevitable bugs that will be discovered.

More lines of code is inherently a bad thing, while I'm all for simplifying the code so that people can understand it, I'm not a fan of doing that at the cost of increasing the amount of maintenance that will be needed on the code base overtime.

Re:Warming up to Go by actsasbuffoon on Saturday September 12, 2015 @10:55AM (#10208238)
> But the expense of extra lines of coke come at significant maintenance costs.

That is the single greatest typo I have ever read.

Re:Warming up to Go by to3m on Saturday September 12, 2015 @11:44AM (#10208372)
Also in the running, this priceless slip: https://news.ycombinator.com/item?id=3726842
Re:Warming up to Go by z92 on Saturday September 12, 2015 @11:59AM (#10208402)
Looks like that one was later corrected. Wondering what the mistake was originally.
Re:Warming up to Go by ptomato on Saturday September 12, 2015 @12:13PM (#10208441)
still there - the inadvertent your mom joke in last sentence with "uses" instead of "users"
Re:Warming up to Go by tomcam on Saturday September 12, 2015 @05:42PM (#10209485)
So does your mom.

Pretty sure this will get flagged, and I hate myself for it, but it will make sense when you review that post again.

Re:Warming up to Go by notNow on Saturday September 12, 2015 @11:40AM (#10208362)
Code is coke at least this is true for some hardcore people here.
Re:Warming up to Go by FilterSweep on Saturday September 12, 2015 @12:14PM (#10208444)
And its wonderful to see. I'm humbled a̶l̶m̶o̶s̶t̶ every time I read HN.
Re:Warming up to Go by PuerkitoBio on Saturday September 12, 2015 @11:13AM (#10208299)
> But the expense of extra lines of co[d]e come at significant maintenance costs.

All lines are not created equal.

    if err != nil {
        return err
    }
Those 3 lines have very, very little maintenance cost.

The obvious must also be mentioned: less lines of code usually mean building on abstractions - the lines of code are in there, somewhere, but not in your code. Better hope that abstraction is well-tested. Of course it's all a matter of balance, but I disagree with "more lines of code is inherently a bad thing", which stated another way, would mean "cram as much information as possible in a single line of code".

In the end, lines of code are a bad metric of software.

Re:Warming up to Go by saurik on Saturday September 12, 2015 @11:21AM (#10208317)
Less than other lines, but I definitely would disagree with the doubled-up "very" on "little". In addition to simply being distracting and verbose, a core issue is that you have to make certain you always have the boilerplate and that you have to make certain it is always correct. Otherwise you just https://gotofail.com/ :(.
Re:Warming up to Go by Daishiman on Saturday September 12, 2015 @11:59AM (#10208403)
Those are some truly bullshit lines there.

Totally unnecessary, a solved problem eons ago through very manaeable, existing constructs that have a lot more semantic meaning.

Re:Warming up to Go by jacques_chester on Saturday September 12, 2015 @11:30AM (#10208335)
> Those 3 lines have very, very little maintenance cost.

These three lines are everywhere, give me no clear indication of whether they are intentional behaviour or merely a boilerplate-by-convention, and worst of all, they obscure the actual business logic.

Re:Warming up to Go by PuerkitoBio on Saturday September 12, 2015 @12:08PM (#10208428)
Error handling does not obscure the business logic, it is very much part of the business logic.
Re:Warming up to Go by moe on Saturday September 12, 2015 @12:31PM (#10208483)
Error handling does obscure the business logic when, as in Go, nearly every operation needs to be followed by a boilerplate guard clause.

Other languages (e.g. Java) have solved this problem quite elegantly over a decade ago with the introduction of Checked Exceptions[1].

Since this is the pattern that every Go program ends up emulating anyway, Google could save everyone a lot of work by just baking it into the language.

In the eternal words of Larry Wall:

  The computer should be doing the hard work.
  That's what it's paid to do, after all.
[1] https://en.wikipedia.org/wiki/Exception_handling#Checked_exc...
Re:Warming up to Go by nickbauman on Saturday September 12, 2015 @08:43PM (#10209935)
I think almost 50% of my Go code is checking for errors. Every time I see:

foo, err := somebullshit()

if err != nil {

  // bleargh
}

I feel as though I've experienced a very slight but irrevocable grand mal seizure.

Re:Warming up to Go by fooster on Saturday September 12, 2015 @12:42PM (#10208524)
Sorry, but if you have every operation followed by a guard clause you are doing it wrong.

For example,

https://blog.golang.org/errors-are-values

Re:Warming up to Go by lmm on Saturday September 12, 2015 @01:19PM (#10208621)
That's the right approach, but it's still decades behind what a decent programming language would give you for free. You have to copy-paste that "errWriter" definition for every possibly-error-raising method that you want to call, because go has no generics. If you want to write to two different writers, let alone a list of them, then you have to go back to the function-and-variable version, which is very boilerplatey and impossible to reason about when refactoring.
Re:Warming up to Go by jacques_chester on Saturday September 12, 2015 @12:55PM (#10208554)
This just shows that, to avoid having do-and-check after every operation, the provider needs to invent several idioms because it's tedious to the consumer.

But now the consumer needs to know multiple idioms and recognise when they are being used. And the reader of the consumer code needs to know that the normal `err != nil` idiom is not being followed. All of which requires everyone involved to do more reading of source code than a "throws" line.

How is this simpler than having a single, universal concept of exception handling?

Re:Warming up to Go by moe on Saturday September 12, 2015 @01:01PM (#10208578)
Your link describes (and encourages) exactly the laborious emulation of Checked Exceptions that I was talking about in my above comment.

It doesn't make the guard clauses disappear. It merely forces the programmer to manually aggregate them at a higher level with even more boilerplate code.

Re:Warming up to Go by mahyarm on Saturday September 12, 2015 @06:09PM (#10209566)
If my guard clause is doing the same thing for 20 lines, it increases readability. Could be an optimization point too!
Re:Warming up to Go by ivank on Saturday September 12, 2015 @12:57PM (#10208562)
> https://blog.golang.org/errors-are-values

The "helper" function there introduces 10 lines of code (per function in which you want it), and doesn't even work if you call anything besides that one `write` in sequence.

Re:Warming up to Go by Roboprog on Saturday September 12, 2015 @02:28PM (#10208837)
Mostly agree, but I'm sure you must have meant UN-checked exceptions :-)

  if ( somethingICanDoNothingAbout)
      { throw new UncheckedGameOverException( "You're screwed!") }
...

  // top level event/request/message handler, many layers up
  catch ( Throwable e)
      { log.error( "Game over:", e); ... }

(I'm with the Anders/C# camp on this one, as much as I dislike MS otherwise)
Re:Warming up to Go by moe on Saturday September 12, 2015 @04:01PM (#10209133)
No, I mean Checked Exceptions. Unchecked Exceptions are the devil and should be abolished.

The inclusion of Unchecked Exceptions is one of the biggest warts on the Java language.

Re:Warming up to Go by nickbauman on Saturday September 12, 2015 @08:39PM (#10209924)
Unchecked exceptions are for programmer errors. They are an important part of the language since humans (who write the programs) are error prone. They are unrecoverable by nature. Checked exceptions should only exist in situations where the caller can definitely recover from them. Arguably almost nobody does the latter correctly when choosing checked exceptions and the consensus is, as a result, this is the kind of exception that should have been omitted from the language.
Re:Warming up to Go by moe on Saturday September 12, 2015 @09:29PM (#10210021)
Unchecked exceptions are for programmer errors. They are an important part of the language since humans (who write the programs) are error prone.

I should clarify: The wart is that programmers can introduce their own unchecked Exceptions. This should not be allowed since, as you say, unchecked Exceptions only make sense for unrecoverable runtime errors.

Checked exceptions should only exist in situations where the caller can definitely recover from them.

False dichotomy. Unchecked exceptions become part of the callee's method signature, i.e. the contract that the caller must fulfill.

Arguably almost nobody does the latter correctly when choosing checked exceptions

Baseless claim. I see many people using Exceptions correctly and elegantly, for error handling and flow control.

and the consensus is

Opinion != consensus.

Re:Warming up to Go by Roboprog on Saturday September 12, 2015 @10:37PM (#10210162)
There are times when Go (et al) style multiple return values would better serve a function than exceptions.

For example, a function to parse a string into a number should return both a number-reference, and a flag of some kind (or maybe just an Option). Crap input should not generate an exception like out-of-memory or an I/O error. Trying to dereference the number w/out checking to see if the string was parseable COULD generate a not-mandatory-to-check exception, though.

I guess there is a certain amount of tension between the idea that functions/methods should document the kind of errors/exceptions they might have, and between the annoyance that is all the crappy do-nothing catch clauses that turn around and re-vomit wrapper exceptions that ultimately land in a "well, it didn't work" log message and punt, rather than really "handling" the original exception in any way, shape or form.

Re:Warming up to Go by pcwalton on Saturday September 12, 2015 @12:43PM (#10208530)
> Error handling does not obscure the business logic, it is very much part of the business logic.

That's why it's nice when the language provides tools to make it more likely to be correct.

Re:Warming up to Go by jacques_chester on Saturday September 12, 2015 @12:16PM (#10208446)
If your business is handling full disks and HTTP timeouts, sure, exceptions and business logic are the same.
Re:Warming up to Go by jerf on Saturday September 12, 2015 @01:24PM (#10208636)
The further away from a "network server" you get, the less appealing Go gets. It's a general purpose language, but it's not entirely wrong to think of it as a DSL for writing network servers that happens to be useful for other some things.

Why doesn't lack of generics kill Go? Well, if you're working in Go's wheelhouse, []byte actually covers a lot of things and you're rarely that far away from it, because you're about to read or write a []byte real soon now. (I don't mean you always have one literally in hand, you probably marshaled in into a struct, just that you're often very close to either reading or writing it.)

Why does it work to have this error handling in Go? Well, when writing network servers or other basic cloud infrastructure, actually a lot of the errors require different handling on a case-by-case basis. Big ol' exception blocks around the whole operation are often hiding bugs, or at the very least, suboptimal exception handling. By contrast, when you're not writing the code to do all the nitty-gritty network operations it's a level of detail you don't want or need.

I think every single posted "success story" I've seen for Go has been a network server. I don't think I've ever seen one about how it really cleaned up my Android app, or how my game development was crashing and burning until I switched it to Go. I don't think this is a coincidence, nor do I expect to see one anytime soon, the recent Android support notwithstanding.

Re:Warming up to Go by lmm on Saturday September 12, 2015 @01:27PM (#10208645)
The most common maintenance task is reading an entire region of code, which costs in direct proportion to the number of lines. Bugs are also proportional to number of lines of code independently of language, http://programmers.stackexchange.com/a/185684 . So no, lines of code is a good metric.

(Though I wouldn't characterize it as "cramming"; better languages save lines by not requiring programmers to specify irrelevant detail rather than by packing more information into the same space).

Re:Warming up to Go by kasey_junk on Saturday September 12, 2015 @11:33AM (#10208347)
I routinely find bugs in golang error handling code. In general I agree with your point but this is not the example I would use.
Re:Warming up to Go by bkeroack on Saturday September 12, 2015 @12:04PM (#10208416)
As opposed to the bugs you didn't find in some other language, because your program was spitting out unhandled exception errors from random places at runtime.
Re:Warming up to Go by masklinn on Saturday September 12, 2015 @12:07PM (#10208423)
An unhandled exception means the program stops rather than continue in an unexpected and possibly invalid state. The latter being what happens in C or Go.

And it's not like these two are the only possible error handling strategies.

Re:Warming up to Go by fooster on Saturday September 12, 2015 @12:39PM (#10208514)
That isn't really what happens most often. What happens is that the exception is caught and thrown out, or caught and printed (especially with java if you are using eclipse_.
Re:Warming up to Go by eropple on Saturday September 12, 2015 @03:18PM (#10208992)
Why would I spit unhandled exception errors from a Try monad?
Re:Warming up to Go by fithisux on Saturday September 12, 2015 @11:52AM (#10208388)
These three lines have made my life easier.
Re:Warming up to Go by incepted on Saturday September 12, 2015 @01:39PM (#10208688)
... and your code more fragile.

You can't have your lunch and eat it too: if you want robust code, you -- the developer -- need to spend some time thinking about how you manage your errors. Go makes it all too easy to sweep those under the rug.

Re:Warming up to Go by cdelsolar on Saturday September 12, 2015 @07:34PM (#10209763)
No it doesn't. You need to handle every error or your program will panic and crash. You are forced to think of and handle every error by the if err != nil construct.
Re:Warming up to Go by mahyarm on Saturday September 12, 2015 @06:01PM (#10209546)
The problem with verbose code, is missing the small difference in the boilerplate and creating new bugs.

It's the same problem with copy/paste code. With copy/paste code, it often has small changes that you can miss easily. You have to read every line of code to not miss the small variations. If the person bothered to generalize the copy paste code a bit, you can cover all cases in one spot, and be less likely to miss problems.

Re:Warming up to Go by ilurkedhere on Saturday September 12, 2015 @03:14PM (#10208980)
Seems like there should be an abstraction for that.
Re:Warming up to Go by bkeroack on Saturday September 12, 2015 @11:23AM (#10208323)
Go is a little more verbose in basically two ways: it forces you to handle error states explicitly, and it doesn't let you do overly-clever things to save a few lines of typing. Neither of which increase the maintenance or testing burden.
Re:Warming up to Go by lmm on Saturday September 12, 2015 @01:23PM (#10208632)
Programming consists entirely of doing overly-clever things to save a few lines of typing. Go belongs to the same school of ludditery as von Neumann's opposition to assembly.
Re:Warming up to Go by jacques_chester on Saturday September 12, 2015 @11:26AM (#10208329)
> it forces you to handle error states explicitly,

Does it?

`if err != nil` is a convention. It's not a checked error. It's like saying "C has explicit array bounds checking", if the convention was to check it by hand.

For all the hate Java gets, when I have a checked exception, I know what it is going to be. I don't need to dig around in sourcecode trying to work out if I should care or not. I don't need to check which of the three Golang error-distinction idioms (package-variable errors, error subtypes, plain-old-errors-with-magical-strings) is in play.

As you can guess, I don't like Go.

Re:Warming up to Go by sagichmal on Saturday September 12, 2015 @12:33PM (#10208489)
> `if err != nil` is a convention. It's not a checked error.

It's... obviously both.

Re:Warming up to Go by jacques_chester on Saturday September 12, 2015 @12:43PM (#10208531)
Sorry, typo. "Checked exception", meaning that the compiler enforces error handling and that you can look a method signature and learn what kind of exceptions the method can throw without needing to read its internals.
Re:Warming up to Go by superuser2 on Saturday September 12, 2015 @02:53PM (#10208912)
'err' is sitting there in the return value and the compiler will force you to either use it or explicitly steamroll it. The difference between C bounds checking and Go error checking is that you can't forget to check an error in Go.
Re:Warming up to Go by azth on Sunday September 13, 2015 @04:29AM (#10210710)
I guess everyone is remembering to check the return values of fmt.Println() then huh?
Re:Warming up to Go by fooster on Saturday September 12, 2015 @12:43PM (#10208526)
And runtime exceptions? What about them?
Re:Warming up to Go by jacques_chester on Saturday September 12, 2015 @12:46PM (#10208538)
They leave you in about the same position as a programmer in a dynamic language.

The advantage of runtime exceptions over Go is that I will get a fatal crash and a stacktrace. I don't have computation proceeding in an undefined, difficult-to-debug way because I forgot to do `err != nil` somewhere (particularly for functions that only return err).

Re:Warming up to Go by fooster on Saturday September 12, 2015 @05:54PM (#10209514)
Or someone will catch it and discard the result which is often often what occurs. For example, with java throw an exception in an executor task (an assertion failure for example). What do you think occurs?
Re:Warming up to Go by jacques_chester on Sunday September 13, 2015 @07:34AM (#10211030)
Which happens in Go too. I've seen a lot of code that happily sails past functions which only return err, meaning the compiler never pipes up.

Don't get me wrong, though. I become grumpy when I see empty catch clauses. Or a logger figleaf.

Re:Warming up to Go by MichaelGG on Saturday September 12, 2015 @02:47PM (#10208901)
>doesn't let you do overly-clever things to save a few lines of typing

So that's what we're calling the lack of HoFs these days? It's really sad to hear that anyone considers map/fold/filter are "overly-clever".

Tim Sweeney (Epic) says[1]:

"“For” loops in Unreal:

40% are functional comprehensions

50% are functional folds"

I hardly see how changing those loops into a construct that more accurately represents them is overly-clever.

1: http://www.cs.princeton.edu/~dpw/popl/06/Tim-POPL.ppt

Re:Warming up to Go by burntsushi on Saturday September 12, 2015 @02:59PM (#10208932)
> So that's what we're calling the lack of HoFs these days?

Go has HoFs. The lack of parametric polymorphism is what prevents you from writing `map` in Go (subject to several caveats surrounding efficiency and type safety).

Re:Warming up to Go by MichaelGG on Saturday September 12, 2015 @04:16PM (#10209197)
In practise I don't find passing function parameters without generics to be very useful.
Re:Warming up to Go by pcwalton on Saturday September 12, 2015 @12:45PM (#10208534)
> Go is a little more verbose in basically two ways: it forces you to handle error states explicitly

No, it doesn't (simple example: the return value of os.Setenv). errcheck exists for a reason.

Re:Warming up to Go by pbnjay on Saturday September 12, 2015 @11:08AM (#10208276)
If you need more tests to cover the "extra lines of code" resulting from the explicit nature if Go code, then your prior tests didn't sufficiently cover all the edge cases. So Go's verbosity is a win.
Re:Warming up to Go by clamprecht on Saturday September 12, 2015 @11:26AM (#10208331)
> But the expense of extra lines of coke

Please tell me that was autocorrect and not muscle memory

Re:Warming up to Go by erkose on Saturday September 12, 2015 @12:45PM (#10208533)
Yikes! Understanding the code is the key to efficient maintenance.