On Fri, Mar 12, 2021 at 1:24 AM Kevin Chadwick <m8il1i...@gmail.com> wrote:

> I find named returns produce more readable code but I avoid them when
> returning a map.
>

Personally, I would recommend avoiding naked returns in all cases. In the
best case, a naked return is less explicit and requires readers to think
about what is returned. In the worst case, a naked return can introduce
subtle shadowing bugs (this has happened in production code, multiple
times).

I don't think the convenience justifies the cost.

Why doesn't go auto init or make an empty map for a named return to avoid
> the potential chance of a panic due to operating on a nil return?
>

It auto-initializes every variable to its zero value. It would be very
strange to make an exception for maps. And named returns aren't really
different from regular variables in this regard.

I also don't think there are many cases where you'd *want* to implicitly
return an empty, non-nil map. A nil map says "there is no map", whereas an
empty map says "there is a map, but it has no elements". It seems very
appropriate that if you want to say the latter, you have to explicitly do
so.

> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/CANNfpqe5VmDfd53YMHDecOEt-XNH3efCVGAa1Z3q6qJJbDQYsw%40mail.gmail.com
> <https://groups.google.com/d/msgid/golang-nuts/CANNfpqe5VmDfd53YMHDecOEt-XNH3efCVGAa1Z3q6qJJbDQYsw%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAEkBMfGwSZrXEsKdH6U%2BpjZzHAOD1_SEeZmcep_SCLgbzWEh5Q%40mail.gmail.com.

Reply via email to