Stupid Question 87: Warning levels, what are they, which level to use, and should they be treated as errors?
I don’t quite remember which blog post it was, or what the exact comment was, but a friend and coworker of mine at Telerik Steve Smith made a comment about warning levels in Visual Studio. That the level should be set to the highest one, level 4. And I do have a vague memory that I’ve heard this a few times before – and also to set treat warnings as errors. But let’s start with the basics.
What are warning levels?
Warning levels are compiler warnings, the code you have written is analyze and problematic code will generate warnings (that can be treated as errors).
There are four levels, with four being the strictest level and also the default level.
0 Turns off emission of all warning messages.
1 Displays severe warning messages.
2 Displays level 1 warnings plus certain, less-severe warnings, such as warnings about hiding class members.
3 Displays level 2 warnings plus certain, less-severe warnings, such as warnings about expressions that always evaluate to true or false.
4 (the default) Displays all level 3 warnings plus informational warnings.
how to change warning levels and how to treat all warnings as errors
How to change the level
To adjust the level of warning you open up the properties setting on the project and change the level under build. Level four is the recommended one, and I have heard from several developers that it is recommended that you treat all warnings as errors. This is not the default setting, but you can set treat all warnings as errors the same place you set the warning level.
See here what happens when you do: (I’ve written up some code that will generate warnings on level 4).
Just a level 4 warning
All warnings treated as errors
Why should you treat all warnings as errors?
It might be a rather good habit to do so, as many devs unfortunately think that the code written is good since it compiles- at the same time it is not a quality check – so even passing the strictest warning level doesn’t prove that the quality is top notch. So you should still write tests and so on, but it can be a great tool to avoid some common bugs in the future, prevent some sloppy code, teach you a few new things and is also a good idea if you are often working as a part of a team.
As for how to ignore certain warnings have a look here: Stupid Question 88: How do I ignore certain warnings in code when I’ve set treat all warnings as errors?
(originally it was in this post, but the post became to long)
Last modified on 2012-11-19