Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a good idea to eliminate compiler warnings?

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 with use strict and use warnings (and often -T as well) to try to achieve the best code quality I can. I understand that a few years ago, the Perl porters group worked hard to make perl itself (the Perl interpreter) compile cleanly under gcc with all warnings enabled. Obviously they felt that was a good idea for code quality. I also understand that nowadays Perl programmers have been adding even more warnings to their code with Perl::Critic, which warns them when they violate best practices found in Damian Conway's Perl Best Practices book (and from other sources, I believe).

I always had a good feeling about code that I had cleaned up this way, but sometimes I couldn't avoid the feeling that some of the work was a little wasted. For example, in my intro C classes over a decade ago, I was taught to start my main() function like this:

void main(void) {

This was minimal and could only be used when you weren't returning a value and weren't accessing your arguments. It works just fine in that case, but gcc warnings would let you know that this function really ought to look like:

int main(int args, char* argv) {

I must've typed a couple of hundred unused int args, char* argv lines back in the day. Did I really make my code better, or just wear my fingers down shorter?

Nowadays I'm programming in Java in Eclipse, and our project has tens of thousands of warnings. I'd like to clean them up. Some of them are especially difficult to understand and eliminate, but slowly I'm learning. A few of these I've had to handle with compiler directives to suppress warnings (usually in tiny minimal methods to factor out the bad practice of ignoring warnings), but I'm finding ways to handle those, as well.

Is this worth a programmer's time? Will a project really be much better if you track down every single compiler warning?

If nothing else, it seems like it'd be nice to reduce the number of warnings to zero so that serious warnings wouldn't get lost in the mess.

Note: Duplicate of this question

like image 322
skiphoppy Avatar asked Feb 04 '09 16:02

skiphoppy


People also ask

Do compiler warnings matter?

They are not errors from the viewpoint of a programming language, but they may be software bugs. However, many compilers can be customized so that their warnings don't stop the compilation process. Warnings must not be ignored. You'd better fix every possible error before starting software testing.

Why is it a good idea to always enable compiler warnings in C?

Not only does handling the warnings make better code, it makes you a better programmer. Warnings will tell you about things that may seem little to you today, but one day that bad habit will come back and bite your head off. Use the correct type, return that value, evaluate that return value.

Should I treat warnings as errors?

Yes, even once in a while you will encounter the occasional warning you'd be better off leaving as a warning or even disabling completely. Those should be the exception to the rule, though. Here's some practical advise: at the start of a new project, start treating all warnings as errors, by default.

Do warnings slow down compilation?

Also, warnings are output, and sometimes, the IDE or the terminal reading the compiler output may be slowed down if you have a lot of warnings.


1 Answers

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.

like image 68
JesperE Avatar answered Nov 16 '22 00:11

JesperE