一般来说,我应该将编译器设置为将警告视为错误吗?

发布于 2024-10-30 14:35:51 字数 61 浏览 0 评论 0原文

从长远来看,修复可能逃脱惩罚的警告带来的短期烦恼是否会带来好处?通过这样做通常可以避免哪些类型的运行时错误?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

橪书 2024-11-06 14:35:51

我的观点是警告的存在是有原因的,忽略它们后果自负。虽然有些人确实很挑剔,但大多数情况下他们这样做是有充分理由的。我宁愿修复它们并进行干净的编译。

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.

一萌ing 2024-11-06 14:35:51

这取决于您认为什么是“您可以逃脱的警告”。还要考虑到您的代码将来的任何修改。

我思考的时间越长,“我可以逃脱的警告”就越少。

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.

别靠近我心 2024-11-06 14:35:51

我通常会修复所有警告,但不要将它们设置为错误......

I usually fix all the warnings, but don't set them as errors...

妄司 2024-11-06 14:35:51

一般来说,是的。

我确信有很多例外。无法以这种方式编译但您不想触及的第三方库头是我能想到的最大的头。 (即便如此,我有时也会在 #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.

仅冇旳回忆 2024-11-06 14:35:51

这就是为什么我认为你应该将警告视为错误:

当你有一长串方法时,某些内容可能会出现 null:

var amount = _client.SnatchLeftoverOrders( _username, _password, "pepperoni").Where( o => o.Ingredients.Any("mushrooms").Where( o => o.Ownersname.ToUpper == _incomingName ).Amount();

或者类似的情况,很多地方都可能发生 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:

var amount = _client.SnatchLeftoverOrders( _username, _password, "pepperoni").Where( o => o.Ingredients.Any("mushrooms").Where( o => o.Ownersname.ToUpper == _incomingName ).Amount();

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文