* Will Faught <will.fau...@gmail.com> [170127 21:58]:
> Variable shadowing is rarely, if ever, a problem for me too; but what about
> for newcomers?
> 
> I think my copy/paste point stands, though; everyone has those problems, at
> least occasionally.

I agree with you.  See my earlier post for my reasoning:

https://groups.google.com/d/msg/golang-nuts/an7f4o-1cjY/UxexDsuDAwAJ

In short, in the multiple assignment case, «var» instead of «:=» adds
one line of code, but it eliminates ambiguity completely.  In

    a, b, c := Foo(d)

what is the cost to every reader of your code to determine which of a,
b, and c are being shadowed?  Look through the entire scope for three
different variable names, at least one of which you are guaranteed to
*not* find, which means you absolutely need to read all the way back to
the beginning of the scope (and hope you don't miss an earlier
declaration which might by var or might be :=).  In

    var b SomeType
    a, b, c = Foo(d)

what is the cost?  Zero!

In the single assignment case, := saves only three characters.  There
may be no ambiguity about which variables are being shadowed, but for
those of us who have experience with a variety of languages, := has
different meanings in different languages; var a = Foo() is clearly a
declaration, but there is significant cognitive load to recognize that
a := Foo() is a declaration, not a simple assignment or something else.
This cognitive load may not apply equally to everyone, but it does apply
to some people, so if anyone else will read your code, be polite and
make it equally readable for everyone:  use the long declaration (which
really isn't long).

...Marvin

-- 
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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to