Derik Whittaker

Syndication

News


Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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,


Posted 02-05-2008 1:45 PM by Derik Whittaker
Filed under: , ,

[Advertisement]

Comments

Peter Ritchie wrote re: Compiler warnings, the trash you forgot to take out
on 02-05-2008 3:09 PM

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.

Damien Guard wrote re: Compiler warnings, the trash you forgot to take out
on 02-05-2008 7:06 PM

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

Kyle Baley wrote re: Compiler warnings, the trash you forgot to take out
on 02-05-2008 8:33 PM

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.

DotNetKicks.com wrote Compiler Warnings, the trash you forgot to take out
on 02-05-2008 11:28 PM

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

Igor Brejc wrote re: Compiler warnings, the trash you forgot to take out
on 02-06-2008 2:49 AM

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).

SitePoint Blogs » .NET on the ???Net Feb 1-7: DataContext Dilemmas & Versioning Databases wrote SitePoint Blogs » .NET on the ???Net Feb 1-7: DataContext Dilemmas & Versioning Databases
on 02-07-2008 6:07 PM

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

.NET on the ???Net Feb 1-7: DataContext Dilemmas & Versioning Databases wrote .NET on the ???Net Feb 1-7: DataContext Dilemmas & Versioning Databases
on 02-11-2008 3:30 PM

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

The Week in ColdFusion: 30 Jan-5 Feb: a bit of everything wrote The Week in ColdFusion: 30 Jan-5 Feb: a bit of everything
on 02-11-2008 5:30 PM

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

Social media opportunities for Web Developers at Case wrote Social media opportunities for Web Developers at Case
on 02-13-2008 4:30 AM

Pingback from  Social media opportunities for Web Developers at Case

http://devlicio.us/blogs/derik_whittaker/archive/2008/02/05/compiler-warnings-the-trash-you-forgot-to-take-out.aspx wrote http://devlicio.us/blogs/derik_whittaker/archive/2008/02/05/compiler-warnings-the-trash-you-forgot-to-take-out.aspx
on 03-28-2008 5:53 AM
Cialis. wrote Metaprolol cialis.
on 08-06-2008 5:17 PM

Cialis 20mg. Cialis.

PzltyFyKFOmUK wrote re: Compiler warnings, the trash you forgot to take out
on 04-13-2009 9:21 PM

H41PQS

Buy percocet online. wrote Percocet.
on 05-30-2009 10:52 PM

Percocet dosing. Canadian prescritions and percocet. Buy percocet online. Potent lortab percocet darvon. How can i identify a percocet 10. Percocet. Percocet shipped cod no prior.

Cheap vicodin. wrote Forum vicodin buy vicodin online.
on 05-31-2009 9:38 PM

Vicodin.

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)