Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

This is possibly one of the worst reasons to use named return values. To be honest, I've always disliked them because they make `return` magical since you now have to read through the whole function twice to make sure that you know what is actually being returned. Luckily you can do explicit returns with named returns, but then it becomes confusing if you have non-explicit returns in the same function.

In my mind there are only two reasons to use named returns. The first is when writing interfaces, and you want to document the returned values in a nicer way (in addition to the godoc). This is mostly a taste thing and is not required.

However, the second one is required by the language and that is deferred function calls that interact with the return value of a function. If you want to have a deferred function call that does some cleanup but has to check the error value of the function (for example a cleanup that only runs if an error occurred) then you have to use named return values. If you want to have a deferred function that changes the return value of the main function then you also need named return values. This is a fairly annoying requirement of Go, but I understand why they felt it was necessary to do things that way to make it feel more explicit.

Other than that, in general named returns make functions harder to read (in my opinion). In a similar vein, I really don't like Ruby's or Rust's implicit returns (though at least Rust is less magical about it).



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: