3

I used to select the default warning level in C++ programming. For example, in VS, the default warning level is Level3 (/W3) and No (/WX-) (don't treat warnings as errors).

I am wondering is it a good practice to choose highest warning level in C++ programming, e.g. EnableAllWarnings (/Wall) with Yes (/WX) (treat warnings as errors)?

Any advice will be much appreciated.

herohuyongtao
  • 141
  • 1
  • 6
  • http://meta.programmers.stackexchange.com/questions/6483/why-was-my-question-closed-or-down-voted/6491#6491 – gnat Mar 17 '14 at 07:17

2 Answers2

8

The compilers are usually very good in spotting mistakes, therefore I always enable all possible warnings and errors (-Wall and -Wextra for gcc), and turn warnings into errors. This way nothing gets through.

Also, I try to write as standard compliant code as possible (-pedantic for gcc), meaning I try to avoid compiler extensions.

Sometimes it is not possible, because of an external library. Then you are forced to put required settings to make it compile. Unfortunately this usually goes with lots of warnings.

BЈовић
  • 13,981
  • 8
  • 61
  • 81
  • Is it worth to fix every single warning, e.g. `signed/unsigned mismatch`? – herohuyongtao Mar 17 '14 at 08:06
  • 2
    @herohuyongtao The latest compilers are quite good. Of course they do have problems. But I tend to believe that they tend to know better then me if there is a mistake. This is not always true, due to compiler bugs, but the cases when it is not true are quite rare. – BЈовић Mar 17 '14 at 08:08
  • "[compilers] tend to know better then me if there is a mistake / not always true, due to compiler bugs" - it's not just about compiler bugs... you may know that your values won't span the range of a data type and therefore no actual signed/unsigned issues will occur (e.g. a vector of students in a class will never have an index that can't be stored in `int`) but it's still sloppy practice, or you may know that you really did mean to say `if (x = y)`... having to write `if ((x = y))` is still a good practice as people who know the compiler warnings may understand that means it's intentional. – Tony Mar 19 '14 at 04:02
  • @Tony That is correct. Compilers are not going to warn you for plain stupidity. That is what unit, functional, integration, acceptance tests are for. – BЈовић Mar 19 '14 at 07:24
4

Generally speaking, yes, it is a good idea to raise warnings to the highest level possible.

You may get some warnings that are overly picky or that just don't worry you in which case you can explicitly pragma them away in specific locations. At least then you are making a specific and explicit decision to ignore the warning.

Warnings as errors is more contentious in my experience. Some people think it is very important to have turned on. I personally keep this turned off to allow a little more flexibility while I am writing code, but then I trust myself not to ignore warnings, and certainly never commit code containing warnings. I have in the past however turned this on to catch programmers who were not quite as conscientious as they should have been.

Claude
  • 41
  • 1
  • 1
    "I have in the past however turned this on to catch programmers who were not quite as conscientious as they should have been." +1! – Panzercrisis Mar 17 '14 at 13:55