Refactoring For Your Code Base

A few days ago I’ve watched a webcast by Brian Button on refactoring. For the most part, he introduces refactoring to the viewer and gives some good examples of why you should refactor and how it’s done. He uses Resharper and the examples really make you want to use the tool. Too bad I’m still using VS.NET 2001 at my current project.


Anyway, the reason why I decided to blog about it is because of some code I just ran into.


.csharpcode
{
font-size: 10pt;
color: black;
font-family: Courier New , Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0px; }
.rem { color: #008000; }
.kwrd { color: #0000ff; }
.str { color: #006080; }
.op { color: #0000c0; }
.preproc { color: #cc6633; }
.asp { background-color: #ffff00; }
.html { color: #800000; }
.attr { color: #ff0000; }
.alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0px;
}
.lnum { color: #606060; }

public string AddData(DataRow dr, DateTime date)
{
    string msg = “”;
    try
    {
        SqlParameter[] paramColl = new SqlParameter[30];
    
        paramColl[0] = new SqlParameter(“@date”,SqlDbType.DateTime);
        paramColl[0].Value = date;
 
        paramColl[3] = new SqlParameter(“@z_1d”,SqlDbType.Decimal);
        paramColl[3].Value = dr[“1d”];
 
        paramColl[4] = new SqlParameter(“@z_1w”,SqlDbType.Decimal);
        paramColl[4].Value = dr[“1w”];
 
    (…)
 
    }
    catch(Exception ex)
    {
        ExceptionManager.Publish(ex);
        throw ex;
    }
    return msg;
}


I simplified the name of the method to add to the effect. But as you can see, there’s room for refactoring. You might want to start with the naming if your variables. As a developer you immediatly can see the parameter date is of type datetime. So you don’t really need to specifiy this concerns a date, as that’s crystal clear! I’d rather know the function this parameter has. Why do I have to supply a date to this method? And what data should the DataRow dr contain? Something you have much less worrying about when using objects. Heck, this would have not been written if an O/R Mapper had been used! 😉


Go view the webcast if you’re haven’t got that much experience with refactoring. You’ll learn a lot. Find the replay of the webcast here.

You may also like...

2 Responses

  1. Eeehm… VS.Net 2002, there is no 2001 version. At least not that I know of, maybe ou have a secret elite version. 😉

  2. At my current location, they were early adaptors, so they got an 2001 version. It’s that good, that we don’t upgrade to 2003. But this also means we can’t use ReSharper…

    (Of course you’re right on this, I just won’t admit it! 😉

Click on a tab to select how you'd like to leave your comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.