5 ms·
I hated the "damnable use requirement" (the error you get on unused imports) for years, but I've been keeping a count of how many bugs they've caught (I was sur
by tptacek 4y ago
I hated the "damnable use requirement" (the error you get on unused imports) for years, but I've been keeping a count of how many bugs they've caught (I was surprised the first time this happened) and I'm up to 3-4 now. What people who code in Go seriously do is just hook `goimports` up to their editor, and then never think about this again.
- morelisp 4y agogoimports seems an endless source of absolutely bizarre bugs unless your packages' terminal path element exactly matches the package name (even sometimes if it does e.g. https://github.com/golang/go/issues/29041 https://github.com/golang/go/issues/29041). These do get addressed over time but also seems to break in a new way at least a couple times a year.
- bobbylarrybobby 4y agoHow is this better than downgrading the error into a warning? You'd be able to build but would still know something was wrong-ish. Certainly a warning would be sufficient to chase down any bugs, and is very freeing compared to an error.
- nouveaux 4y agoThis. Better yet, make this a --strict requirement you can turn on in CI and leave it out for normal development.
- xboxnolifes 4y agoTurned around, what's the benefit of allowing someone to build something that may potentially break, when the alternative is an easy to fix error instead?
- bobbylarrybobby 4y agoThe benefit is not needing to make any additional source changes when you go from using an import to not using it to using it again.
- another_story 4y agoSometimes you want to comment out something and rerun, and having to do the same with dependencies is annoying. Maybe have a dev vs prod compilation split, with the first allowing unused imports.
- tptacek 4y agoI'm not offering any opinion about the use requirement other than to say that it has caught 3-4 bugs for me.
- bborud 4y agoKnowingly shipping code that generates warnings during build is anti-social. It usually isn't that much work to fix a warning as you are writing the code and you encounter the warning the first time. It becomes much more expensive to fix warnings later because you do not have the mental context you had when you wrote the code. But some people behave like that. They think it is someone else's job to chase down bugs in their code after they have knowingly submitted code with defects rather than deal with it immediately. Classifying something as an error rather than a warning makes zero difference to programmers who take care not to knowingly ship defects. It only makes a difference to people who were going to ignore it and let someone else take up their slack. And to be frank: why on earth would you want to accomodate them?
- scott_s 4y agoI think it's important to know what problem the no-unused-imports error is solving. See Rob Pike's talk from 2012, and in particular, the section of dependencies in C++: https://talks.golang.org/2012/splash.article#TOC_5 https://talks.golang.org/2012/splash.article#TOC_5. Basically, the point of no-unused-imports is to reduce compilation time at scale. In the C++ ecosystem, there are lots of redundant and unused #includes. This means that a lot of the bytes sent to the C++ compiler are just thrown away. If you can guarantee that the compiler will never need to look at a source file it does not need to, you can cut down on the compilation time. For small projects, this doesn't matter much. For large projects, it does. Go was designed for large projects.
- sph 4y agoEvery time someone mentions this fact I think about Zig having recently added this "feature" and my blood boils. Unused imports or variables being errors is the most idiotic and overrated paper cut ever implemented in compilers. In no universe it is a good idea. I posit there are actually no or very few bugs it catches, and in exchange it ruins people's iterative development, flow and hyperfocus states when experimenting, because poor compiler can't deal with an unused import, fix it now or else. Now we need an IDE setup to deal with this? And why the hell isn't this a feature that can be turned off? If only I could throw the person that invented this crap into the flaming sun.
- tptacek 4y agoIt has caught some bugs for me, and I essentially never think about it anymore, because `goimports` just handles this. It is substantially easier for me to deal with imports in a Go program than in a Python program, and if I look back at my old code, I wince at all the pointless crap at the top of my programs.