Michael Behan
Michael Behan

Reputation: 3443

In general, should I set my compiler to treat warnings as errors?

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?

Upvotes: 4

Views: 411

Answers (5)

micahhoover
micahhoover

Reputation: 2160

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.

Upvotes: 0

Darren Cook
Darren Cook

Reputation: 28978

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.

Upvotes: 1

mvds
mvds

Reputation: 47114

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.

Upvotes: 2

Ortiga
Ortiga

Reputation: 8834

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

Upvotes: 1

BugFinder
BugFinder

Reputation: 17868

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.

Upvotes: 4

Related Questions