一般来说,我应该将编译器设置为将警告视为错误吗?
从长远来看,修复可能逃脱惩罚的警告带来的短期烦恼是否会带来好处?通过这样做通常可以避免哪些类型的运行时错误?
Does the short term annoyance of fixing warnings that you could get away with pay dividends in the long run? What kinds of runtime errors can typically be avoided by doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我的观点是警告的存在是有原因的,忽略它们后果自负。虽然有些人确实很挑剔,但大多数情况下他们这样做是有充分理由的。我宁愿修复它们并进行干净的编译。
My view is that warnings are there for a reason, ignore them at your peril. While a few really are being picky, for the most part they do so for good reason. I'd rather have them fixed and a nice clean compile.
这取决于您认为什么是“您可以逃脱的警告”。还要考虑到您的代码将来的任何修改。
我思考的时间越长,“我可以逃脱的警告”就越少。
It depends on what you think is a "warning that you could get away with". Also in the light of any future modification of your code.
The longer I think about it, the less "warnings I could get away with" remain.
我通常会修复所有警告,但不要将它们设置为错误......
I usually fix all the warnings, but don't set them as errors...
一般来说,是的。
我确信有很多例外。无法以这种方式编译但您不想触及的第三方库头是我能想到的最大的头。 (即便如此,我有时也会在 #include 行周围使用 #pragmas 来抑制某些标头中的警告,这样我就可以在代码的其余部分中保留警告作为错误。)
另一个例外是小型项目;对我来说,一个简单的经验法则是,如果我需要一个Makefile,那么它就不再是一个小项目,我想了解有关警告、文档和单元测试的信息。
In general, yes.
I'm sure there are many exceptions. 3rd party library headers that won't compile this way but that you don't want to touch are the biggest one I can think of. (Even then, I've sometimes used #pragmas around the #include line to suppress warnings in certain headers, so I can keep warnings-as-errors in the rest of the code.)
Another exception is small projects; a simple rule of thumb for me is if I need a Makefile then it is no longer a small project and I want to get anal about warnings, documentation and unit tests.
这就是为什么我认为你应该将警告视为错误:
当你有一长串方法时,某些内容可能会出现 null:
或者类似的情况,很多地方都可能发生 null 异常。
当您将这些行放入 try/catch 中时,代码比添加 FirstOrDefaults() 然后在 !null 上分支要简单得多。
如果你有一个问题,你必须对 Exception 对象做一些事情,否则它就是一个错误(如果你将警告作为错误处理)。
这不是赢得“精美编程”奖章的方法,但它使事情变得简单。现在的编程中有太多保姆式的东西了。
Here's why I think you should treat warnings as errors:
When you have a long chain of methods where something can come up null:
Or something like that are a lot of places where null exceptions could happen.
The code is much more simple when you put those lines in a try/catch than adding FirstOrDefaults() and then branch on !null.
If you have a catch, you have to do something with an Exception object or it is an error (if you are handling warnings as errors.
This isn't the way to win the "polished programming" medal, but it keeps things simple. There's too much nanny stuff going on in programming these days.