pboyer / antlr4

ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files.
http://antlr.org
Other
26 stars 1 forks source link

s/var bla =/bla := #65

Closed pboyer closed 8 years ago

willfaught commented 8 years ago

?

I use var where I can to match const, func, and type declaration syntax, but I know := is common too.

pboyer commented 8 years ago

If there's a right hand side at the declaration site, := is idiomatic and makes code more robust to refactoring. I only use var for places where control flow leads to different assignments or in the package block where := is not allowed.

willfaught commented 8 years ago

I would argue that it's less robust to refactoring because of unintentional variable shadowing, but you're right that it's commonly done and not wrong.

On Monday, October 17, 2016, Peter Boyer notifications@github.com wrote:

If there's a right hand side at the declaration site, := is idiomatic and makes code more robust to refactoring. I only use var for places where control flow leads to different assignments or in the package block where := is not allowed.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/pboyer/antlr4/issues/65#issuecomment-254245214, or mute the thread https://github.com/notifications/unsubscribe-auth/AAD5Vs6ErBlGxw3LCVzy4cnqm7oM0wtzks5q05cBgaJpZM4KSIOx .

pboyer commented 8 years ago

Isn't variable shadowing an issue regardless of declaration syntax? e.g.

    var foo = 1;
    {
        var foo = "string";
        fmt.Println(foo);
    }
    fmt.Println(foo)

is the same as:

    foo := 1;
    {
        foo := "string";
        fmt.Println(foo);
    }
    fmt.Println(foo)

But maybe you have some other case in mind?

pboyer commented 8 years ago

By the way, my main reason for saying := is more robust to refactoring is it avoids the (usually) redundant var foo Type = .... I really should have mentioned that. This kind of statement does have a semantic difference viz; the type of Foo might be an interface type allowing more general assignment.

pboyer commented 8 years ago

Closing this, but shop talk is still allowed. ;)

willfaught commented 8 years ago

You're right, the := syntax functions the same in the nested block case. Didn't think that one through.

If you paste into the same block, it could be problematic. Parallel assignment meaning could change from (for example) two declarations to one declaration and one assignment (common with reused err vars), or vice versa. The meaning of the statement is based on its context, and that's ripe for copy/paste errors, whereas var decls are unambiguous. If you only use := where necessary (like with parallel assignment with reused err vars), it signals to the reader that you're doing something clever. It's more robust, in my view.

On Monday, October 17, 2016, Peter Boyer notifications@github.com wrote:

Closed #65 https://github.com/pboyer/antlr4/issues/65.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/pboyer/antlr4/issues/65#event-826263211, or mute the thread https://github.com/notifications/unsubscribe-auth/AAD5VrHlA05tFtcDBOk41OdEGFIwZDdgks5q06xTgaJpZM4KSIOx .