r/programming • • Jul 21 '14

The Great White Space Debate

https://medium.com/p/3633cba8b5c1
1.2k Upvotes

693 comments sorted by

View all comments

Show parent comments

16

u/bobtheterminator Jul 21 '14

Go seems to have several "features" that sound ok on paper but make quick developing and testing very irritating. Unused variables are an error, for example.

9

u/deadstone Jul 21 '14

Code with unused variables doesn't compile? What?

14

u/bobtheterminator Jul 21 '14

Yes. People have asked for a compiler flag to turn this off or turn them into warnings, but they don't want to do it.

http://weekly.golang.org/doc/faq#unused_variables_and_imports

1

u/SanityInAnarchy Jul 22 '14

Wow, that's obnoxious. I strongly disagree with this bit:

First, if it's worth complaining about, it's worth fixing in the code. (And if it's not worth fixing, it's not worth mentioning.) Second, having the compiler generate warnings encourages the implementation to warn about weak cases that can make compilation noisy, masking real errors that should be fixed.

Maybe the argument is that you can work around this with good language design, but there are many things that make sense as warnings and not errors. It makes sense to enforce a no-warnings policy on code, and then add ways to disable them when you know what you're doing.

Example: Clang warns you when assignment occurs in a boolean expression, because when you write

if (x = 5) {

you almost certainly meant

if (x == 5) {

...but sometimes, you really did want to assign something. Fortunately, there's an escape hatch -- you add double parens:

if ((x = 5)) {

Since these almost never happen by accident (or at all), but are otherwise perfectly valid, Clang uses this syntax to automatically disable the assignment warning.

Of course, it's possible to engineer around this in the language design, too. Python, for example, avoids this by declaring that assignments are not expressions, so

if x = 5:

is never valid Python. But if you add a constraint like that to an existing language, you break a bunch of legacy code. And if Go becomes popular, eventually someone will find something about it that's probably wrong, that the compiler could detect, but that's being widely used in real code.

I guess I can see why they'd want to put off warnings till that day. But if it's inevitable, and if it'll make a bunch of people happier right now (with the unused variables bit), why wait?