Is it a good idea to eliminate compiler warnings?

前端 未结 14 2660
既然无缘
既然无缘 2021-02-19 01:00

In the past I\'ve worked with -Wall and other switches for gcc to eliminate every compiler warning for projects I\'ve been involved in. Similarly, in Perl, I always program wit

相关标签:
14条回答
  • 2021-02-19 01:34

    I'm going to move against the pack here and say that there may be some compiler warnings that aren't worth fixing. Example follows:

    We have a great deal of code that was written prior to Java 1.5 and generics. It still works. Eclipse also generates literally thousands of warnings about unparametrized collections. Practically all of these collections are limited in scope; they're never going to break. But wait, you may ask; what about the few collections which aren't limited in scope? Aren't you risking a collection somewhere containing an instance of something that should never go in there?

    The response is: a lot of that code is also unchanged. It's legacy-ish; no new code is modifying those collections, and that code has been used for years without problems. It's still in our code base, and we would have to maintain it if a bug manifested, but in practice, it's done its job. Spending the time necessary to parametrize all of those generic collections would take time out of other needed projects.

    Note the caveats here, then:

    • The code has been used for a long time with no bugs reported.
    • The code is not undergoing any substantial editing.

    If either of these stops being true - say, for instance, a bug suddenly does manifest, and we have to go in and edit - the warnings suddenly become more worthwhile to fix. (Incidentally, they're very inexpensive to fix at this point, too; we can fix the relevant blocks along the way as we fix the bug.)

    0 讨论(0)
  • 2021-02-19 01:37

    Yes, definitely. A warning is an indication that something may be wrong in your code. Either you fix it, or you should disable the warning -- if you're not going to act on it, having the warning is just preventing you from detecting when new warnings appear.

    Easiest way to get and keep the code warning-free is simply to always compile with -Werror (/WX in Visual Studio) or equivalent.

    0 讨论(0)
  • 2021-02-19 01:38

    Yes, do so..

    Just yesterday I did that for a project at work. While removing the warnings I came about two issues that turned out to be real bugs.

    It was a C-project, and in one source the sqrt (square root) was called. I got the following warning:

    test.c:4: warning: implicit declaration of function 'sqrt'
    

    Turned out that the coder forgot to include the correct header-file and the square root was called with integer arguments, not floats. This was obviously wrong and not what he intended.

    The warning has been there for quite a while, but noone has seen it because it was lost in the other 1000 harmless warnings that just scrolled over the screen. Warnings are there for a reason.

    Btw - fixing the warnings took me like ... 4 hours ... That's nothing compared to the time it took to write all that stuff.

    0 讨论(0)
  • 2021-02-19 01:39

    Eclipse, IntelliJ and other modern code quality systems have huge numbers of available warnings.

    Cleaning up warnings is a useful practice, if for no other reason than to quiet down the noise. Your code probably already has some of these that are real bugs.

    However, many of the warnings are probably bogus. Some warnings from Eclipse are things like micro-optimizations or stylistic nitpicking. This means that some of your cleanups must be tweaking the settings of your tool, rather than code changes or localized suppression directives.

    0 讨论(0)
  • 2021-02-19 01:47

    Another reason to ensure that your code compiles with no warnings is that, if someone else* has to make changes to the code at some point in the future, a good place to start is to make sure that they can compile the code in its current form.

    However, if they do this, and see lots of warnings, are these warnings that have always existed, or have they installed the compiler incorrectly on their PC?

    At least if there's a rule (and a box to tick on the checklist, if you use one) that the code must compile without warnings, then you avoid this ambiguity.

    (* Someone else may include your future self).

    0 讨论(0)
  • 2021-02-19 01:48

    Just to add warnings, here is what a gnome developer use in his code:

    http://blogs.gnome.org/otte/2008/12/22/warning-options/

    0 讨论(0)
提交回复
热议问题