It’s a .NET Life

December 10, 2008

Code Noise!!!

Filed under: .NET, Agile, Refactoring — Tags: , , — david.yancey @ 11:16 pm

If 4 letter words immediately come to mind when you do a code review…then you might have dirty code.

If you wince each time a new feature is requested to be added to your existing library of code…then you might have dirty code.

If the amount of time it takes to research a bug is inversely proportional to the amount of time you took to develop said bug…then you might have dirty code.

Alright enough of my attempts to making “redneck jokes” about dirty code.  Lets take a look first at what defines dirty code.  Then we’ll take a look at some tips/tricks to refactor dirty code into what would be considered clean code.

What makes code dirty?

Bad Names:

Take a look at the code below and try to determine what each variable represents.  You’ll be able to after studying the code for a few minuets, but as programmers we should write our code so that the intent is revealed not just in the method names but also the parameter names and variable names.

private static XDocument BuildXml(IEnumerable<v> vL)
        {
            XDocument x = new XDocument();
            XElement r= new XElement("searchResults");
            XElement b= new XElement("book", vL.FirstOrDefault().BookName);
            results.Add b
            foreach (var v in vL)
            {
                XElement v1 = new XElement("verse",
                              new XElement("chapter", v.Chapter),
                              new XElement("verseNumber", v.VerseNumber),
                              new XElement("verseText", v.Verse)
                     );
                results.Add v1
            }

            returnXML.Add(r);
            return returnXML;

        }

 

Noisy Comments:

We’ve all seen these comments, probably most of us are guilty of creating them.  Noisy comments are comments that serve no other purpose other than to repeat what has been stated in the code.  Comments should reveal intent that is not apparent in the code.

/// RandomVerse
/// returns a random integer
/// uses no parameters
private static int RandomVerse()
{
    return GetRandomNumber(1, 30000);
}

 

Busy Methods:

Busy methods are those which do to much.  Methods need to follow the Single Responsibility Principle which says that a method should do one thing, do it well, and do only that one thing.  Well the SRP is commonly applied to Class’s but I think it fits Methods as well.

There are lots of articles, blogs, books on what dirty code looks like and what to do with it. 

Coding Horror:
Jeff Atwood has  quite a few good articles on the subject.

Smelly Code

Curly’s Law

 

Robert Martin (Uncle Bob)

Robert Martin has written a book on the subject of Clean Code.  Which I highly recommend.

 

What to do with Dirty Code?

Refactor!!!

 

to be continued…

 

David Yancey

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • DotNetKicks
  • Technorati
  • LinkedIn
  • Live

1 Comment »

  1. [...] Dirty Code!!! (David Yancey) [...]

    Pingback by Dew Drop - December 11, 2008 | Alvin Ashcraft's Morning Dew — December 12, 2008 @ 5:06 am

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress