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
Dead Code: Code Smell #1

In my humble opinion the worst code smell you can have is Dead Code.  I know that other smells such as large methods, large classes, overly complex logic, interwoven dependencies, etc are all smells, but nothing, I MEAN NOTHING smells worse than dead code.  Let me explain.

**** BTW, I am not talking about commented out code (which is another smell), I am talking about code that is actually executed, but not used *****

In most systems today (even the good ones) code is not accurately commented or documented, if it is commented or documented at all.  As you are looking though code and you run across logic that 'looks needed' but is not being used, what do you do.  I assume you have to stop and ask yourself 'Should this be used'?

Here is a classic example of why dead code smells.
Lets say you are on the hunt for a pesky bug and you stumble into a method that you think is the cause.  As you are stepping though and reading the code you notice some dead code.  At first you pass it by thinking it is not needed, then you stop and take a double take.  At this point you realize that code may be useful, but since it is not being used, you have to start making assumptions.  Is the dead code an accident?  Was it done with intent? 

Now what do you do?

We all know that when you start making assumptions you are destined for trouble.  A good developer has to think that the previous author had good intent when they made their code changes, but since the code is left over, you are not sure what to do.  Because the dead code is left over you are at a cross roads, should I delete it, or should I try to determine how it was meant to be used.  It is here you get into trouble.  You are forced to try to put together the puzzle without enough information to make a educated guess.  If the original author would have just removed the dead code, the new author would be free to make his/her own decision on how to solve the problem at had.

One other thing about dead code, it wastes cycle time.  You are slowing down your application by executing code that is not used.

Moral of the story, if you are not using the code delete it.  Or, if you must, comment it out and tell WHY you are not using it so the next guy does not have to make assumptions.

Till next time,

***** Note, if you are using Resharper, it will flag dead code for you.  This will make removing it much easier *****


Posted 02-14-2008 5:53 AM by Derik Whittaker
Filed under: ,

[Advertisement]

Comments

» Daily Bits - February 14, 2008 Alvin Ashcraft’s Daily Geek Bits: Daily links, development, gadgets and raising rugrats. wrote » Daily Bits - February 14, 2008 Alvin Ashcraft’s Daily Geek Bits: Daily links, development, gadgets and raising rugrats.
on 02-14-2008 9:00 AM

Pingback from  » Daily Bits - February 14, 2008 Alvin Ashcraft’s Daily Geek Bits: Daily links, development, gadgets and raising rugrats.

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)