Iris Classon
Iris Classon - In Love with Code

Stupid Question 87: Warning levels, what are they, which level to use, and should they be treated as errors?

[To celebrate my first year of programming I will ask a ‘stupid’ questions daily on my blog for a year, to make sure I learn at least 365 new things during my second year as a developer]

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)


Leave a comment below, or by email.
11/21/2012 12:39:26 AM
Along the same vein, enabling code analysis on your visual studio project usually reveals a few more warnings that might be good to know about. 
11/21/2012 4:33:31 PM
I worked at one company that tried enforcing setting the warning levels to the maximum level. There was one senior engineer who believed that all warnings should be resolved, and he was a member of the methodology police. There were some warnings that we couldn't fix because of the VC compiler at the time  (Visual C++ 6). That didn't last long.

I think it can be useful for some cases, but it also depends on what you're trying to accomplish. I think that you can turn on all warnings, but leave out the option to treat them as errors. 

Last modified on 2012-11-19

comments powered by Disqus