Should a Variable's Type Come After Its Name?
Canonical engineering manager Ben Hoyt believes that a variable's name is more important than its type, so "the name should be more prominent and come first in declarations."In many popular programming languages, including C, C++, Java, and C#, when you define a field or variable, you write the type before the name. For example (in C++): // Struct definitionstruct person { std::string name; std::string email; int age;}; In other languages, including Go, Rust, TypeScript, and Python (with type hints), you write the name before the type. For example (in Go): // Struct definitiontype Person struct { Name string Email string Age int} There's a nice answer in the Go FAQ about why Go chose this order: "Why are declarations backwards?". It starts with "they're only backwards if you're used to C", which is a good point - name-before-type has a long history in languages like Pascal. In fact, Go's type declaration syntax (and packages) were directly inspired by Pascal. The FAQ goes on to point out that parsing is simpler with name-before-type, and declaring multiple variables is less error-prone than in C. In C, the following declares x to be a pointer, but (surprisingly at first!) y to be a normal integer: int* x, y; Whereas the equivalent in Go does what you'd expect, declaring both to be pointers: var x, y *int The Go blog even has an in-depth article by Rob Pike on Go's Declaration Syntax, which describes more of the advantages of Go's syntax over C's, particularly with arrays and function pointers. Oddly, the article only hints at what I think is the more important reason to prefer name-before-type for everyday programming: it's clearer. Hoyt argues a variable's name has more meaning (semantically) - pointing out dynamically-typed languages like Python and Ruby don't even need types., and that languages like Java, Go, C++ and C# now include type inference. "I think the takeaway is this: we can't change the past, but if you're creating a new language, please put names before types!"
Read more of this story at Slashdot.