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.

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; }
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](