Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Derik Whittaker

Thoughts on Software Development, .Net, OOP, Design Patterns and all things cool



Compiler warnings, the trash you forgot to take out

There was once a time where I did not care about compiler warnings, but times have changed.  In fact, I remember having a debate with a fellow developer some time ago about why compiler warnings were not that bad..... Oh how times have changed.

In my opinion (my changed opinion) compiler warnings are just a symptom of a bigger problem.  When you really stop to look at the warnings you will see that most of the time they are trash left over from development tasks of yesterday.

Lets take a look at some common warnings.

  • The private field 'object.property' is never used
    This is trash with a capital T.  This means some developer once used/need/thought they needed a field or property and turns out they were wrong. 

    Why is this trash, simple.  What happens when some new developer comes along and sees the field/property and thinks 'hey, this is exactly what I was looking for'.  But what he (could be she, but I choose he) does not realize is that the field/property is NEVER being used.  this can cause all sorts of headaches for him.  If the property was not there, he would have to think more and fully understand what he was implementing and thus allowing for his time to be better spent.

    Make sure you delete any unused fields/properties as it will save time and headaches.

  • The variable xxx is declared, but never used
    This is also trash.  This is very similar to the 'field never used' warning above.

    Make sure you delete any unused fields/properties as it will save time and headaches.

  • The private variable xxx is assigned, but never used.
    This tells me that someone thought they were going to do something with a value, but decided not to.  Chances are that this variable is not even needed and is just wasted code.

    Remove these or make sure they are being used.

  • The using directive for 'some namespace here' appeared previously in the namespace
    This is not a major issue, just says someone added the same statement multiple times.  Remove the duplicate.

    Remove this as it makes for less code and is cleaner.

  • Object.Method is obsolete, it has been replaced by AnotherObject.Method
    This can become a major issue down the road.  Typically methods are marked obsolete for a reason.  That reason may be because the intent has changed or because it is no longer supported. 

    Now I know that there are times where you still need/want to call obsolete methods, but try to keep this to a minimum. And for goodness sake COMMENT WHY you are using the obsolete method so the next poor sap is informed.

    Swap the code out for the newer, non-obsolete methods if at all possible.

I know there are more possible compiler warning messages, but you get the point.  Plus I was tired of scanning through the 500+ warnings in our current application.

You may be asking, why the current rant.  Well I will tell you.  Today I have spent a large amount of time trying to figure out why in the hell our application will not compile.  The error that is being spit out is odd, and not very descriptive (not a syntax issue :().  So I decided to start walking through all the warnings to see if something stood out.  Well, because we have 500+ warnings, it was very, very pain full.  I was able to find the 'problem' warning, and once I resolved that issue, things started rolling a lot better.

One rule of thumb I think every team should strive for is to have as little compiler warnings as possible.  I know and understand that it may not be practical to remove ALL warnings, but having a goal to keep the number as low as possible is great.

***** NOTE *****
A lot of these warnings above can be detected with tools like Resharper and thus be easily avoided.

Till next time,



Comments

Peter Ritchie said:

Amen--fixing all warnings is a good thing.  (although, simply "hiding" them is bad, very bad--which is probably where "warnings aren't important" POV came from).

I was involved with one project that had that POV: warnings aren't important.  I made an effort to fix warnings when I started and fixed several bugs in the process.

Apathy for warnings is definitely a bad sign.

# February 5, 2008 3:09 PM

Damien Guard said:

You'll often get warnings you want to suppress if you use reflection to set or read values.

My favourite annoying warning is the one that warns you a variable might not be initialised before use.

Which for me is always on the line:  if (variable != null) ...

Oddly if you set the variable to null on the declaration line the warning goes away...

[)amien

# February 5, 2008 7:06 PM

Kyle Baley said:

I don't think it's unreasonable to strive for zero warnings. I like to start off Greenfield projects by setting Warnings as Errors to true. And its something I work toward on a Brownfield app so that I can eventually turn on the same setting.

# February 5, 2008 8:33 PM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# February 5, 2008 11:28 PM

Igor Brejc said:

The problem can be even worse if you turn on XML documentation generation. Then it is hard to find "important" stuff in a sea of "missing XML doc" warnings.

That's why I decided to track the warning count using CCNet statistics (see igorbrejc.net/.../keeping-track-of-code-comment-warnings-in-your-ccnet-builds).

Our unofficial policy is to decrease (or at least keep the same) number of warnings in each build. Also, you can avoid a lot of this by having FxCop fail the build - a better option than turning "warnings as error" option in VS (FxCop will not fail on undocumented code warnings, VS will).

# February 6, 2008 2:49 AM

SitePoint Blogs » .NET on the ???Net Feb 1-7: DataContext Dilemmas & Versioning Databases said:

Pingback from  SitePoint Blogs » .NET on the ???Net Feb 1-7: DataContext Dilemmas & Versioning Databases

# February 7, 2008 6:07 PM

.NET on the ???Net Feb 1-7: DataContext Dilemmas & Versioning Databases said:

Pingback from  .NET on the ???Net Feb 1-7: DataContext Dilemmas & Versioning Databases

# February 11, 2008 3:30 PM

The Week in ColdFusion: 30 Jan-5 Feb: a bit of everything said:

Pingback from  The Week in ColdFusion: 30 Jan-5 Feb: a bit of everything

# February 11, 2008 5:30 PM

Social media opportunities for Web Developers at Case said:

Pingback from  Social media opportunities for Web Developers at Case

# February 13, 2008 4:30 AM

Cialis. said:

Cialis 20mg. Cialis.

# August 6, 2008 5:17 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Derik Whittaker

Derik is a .Net Developer/Architect specializing in WinForms working out the northern suburbs of Chicago. He is also believer and advocate for Agile development including SCRUM, TDD, CI, etc.

When Derik is not writing code he can be found spending time with his wife and young son, climbing on his bouldering wall, watching sports (mostly baseball), and generally vegging out. Check out Devlicio.us!

Our Sponsors

Red-Gate!