5 ms·
For Go: 1. Exports from packages are to be defined with a Capital letter. To me, that is very hard to read. An explicit export statement is way better, if verb
by kumarvvr 7y ago
For Go:
1. Exports from packages are to be defined with a Capital letter. To me, that is very hard to read. An explicit export statement is way better, if verbose.
2. Type declarations after variable (x int, y int), to me personally, is hard to read. Now I know a lot of people are fine with it, so this is a personal thing for me.
3. Multiple return values syntax is terrible.
func split(sum int) (x, y int) {
x = sum * 4 / 9
y = sum - x
return
}
In this function, the sum is the input and x & y are outputs. The syntax seems weird to be. This also is a personal gripe.
4. Variable declarations can be var i, j int = 1,2 AND k:= 3 with implicit typing. To me this is a bad design, with various programmers choosing different methods to declare and initialize. Leads to a lot of cognitive overhead to read code, especially on large open source projects. Why not make it explicit to declare types? Sure, it's a bit of more work for the programmer, but in the long run, that bit of more initial work will save a lot of downstream confusion and frustrations.
5. Constants cannot be declared using := syntax. I am sure there is a valid reason for it, but assignment in the language for now is = , := and those can and cannot be used in various places. That is confusing.
6. For loop has no parenthesis for the loop initialization but needs {} for the loop blocks. This is really stupid. Personally, for(i:=0,i<100;i++) {} is a concise syntax and looks pleasing and readable.
7. To complicate it further the initialization and post statements are optional
// This is a valid for loop in Go
sum := 1
for ; sum<1000; {
sum = sum + 1
}
Isn't that confusing in itself. Sure, we don't have to write a loop that way, but some junior developer is going to write it anyway, and it muddles up the code for sure.
8. From the official Go Docs "At that point you can drop the semicolons: C's while is spelled for in Go"
// This is a ALSO valid for loop in Go
sum := 1
for sum<1000 {
sum = sum + 1
}
yay.
9. And then there is this gem in the "if" statement.
if v := math.Pow(x, n); v < lim {
return v
}
Yes, you can write an optional initial statement before the if condition. All this for what? So you save one extra line in the code? Sorry, the beards got too long and too tangled up for whoever came up with this idea.
10. Also, the initial statement you saw just after the if statement, the variable "v" is available in the scope of else/if and else statement blocks. But not outside the if/else scope.
Yay, another way to confuse code readers.
10. What is up with the defer keyword to defer function calls until the "surrounding function returns".
There are many more, but I think I have made it clear why, personally, Go is off putting.
edit : Changed format a bit.
- tomp 7y agoTry prefixing code with two spaces instead of a ">" sum := 1 for sum < 1000 { sum = sum + 1 }
- nemo1618 7y agoUppercase exports are great once you get used to them. You can know whether an identifier is exported instantly without having to look up its declaration. Agree that the multiple declaration/assignment syntaxes are unfortunate. Another example of this is that := can't be used for top-level declarations. Once you learn the rules you stop having to think about it, but if the rules were more consistent, they'd be easier to learn. The other big issue with Go's variables is shadowing. Again, one of those things that you pick up fairly quickly, but will bite newbies a few times. If-declarations are nice because they restrict the scope of a variable. I like knowing that I can see instantly that a particular variable is only used in three lines. Defer is a great way to clean up resources without the complexity of destructors and exceptions. Defer is also ubiquitous in code that needs to wrap accessor functions in a mutex. It's been adopted by a few other languages, which shows that it has merit. My only gripe with defer is that it only works on function scope; it'd be nice to have a defer scoped to a for loop. The other stuff is your personal preference, which I disagree with, but can't argue against :) I'll add another personal gripe of mine, though: typecasting is indistinguishable from a function call, e.g. Foo(bar) might be calling Foo on bar, or casting bar to type Foo. Those should really have different syntax.
- traes 7y agoDon't know Go but I'm going to give it a shot: 1. Casing is a personal preference, so that's fair, but not a valid criticism. 2. Type decls after the variable are much easier to parse, and allow for (easier) type inference. 3. Yeah? References exist... C/C#/C++ have them too. 4. This ones a fair criticism. 5. That's because constants have a completely different syntax from variable assignment. 6. The parenthesis are syntactically unnecessary and add nothing, the curly braces are required for an unambiguous parser (barring significant whitespace). 7. This is true in almost every language with for loops. Try it in the JavaScript console in your browser. 8. Yeah? So? 9. The scoping is a feature, not a bug. E.G. if v := getResult(); v == Success { Print("Success!") } else { Print("Failed with error code ", v) } // v doesn't pollute namespace out here 10. That one I'm not educated enough to make a reply to.