Why do we refactor immediately?

I ended up having to go back through some legacy code, if you can call 2 year old code legacy. It was something built by a vendor. One issue with the original build was a need to change the look and feel based on a particular product dealer.

The solution employed, by the vendor, was to create a folder with dealer graphics and then attach it in IIS. While I have known it was here, this is the first time it bit me in recent memory. The proper solution would have been to store the metadata in a database (done) and use themes. It is rather elegant and very easy to hook up. Seeking an external folder, made internal by IIS trickery, is a train wreck waiting to happen.

There are two lessons here:

  • When in doubt, ask
  • Refactor early and often

When in Doubt Ask

The solution employed fails when you have not hooked up the directory in IIS. This means on local development, you have to go through the steps of hooking everything up in IIS prior to developing. Not the greatest solution, as I cannot develop off a file path. Not a huge deal, overall, but having to set up each dev machine could be a pain in some companies. Not good.

Themes have been around for quite a few years and work rather easily. One easy way is to create your own page base, from Page, and have it do the dirty work. You need only do this once per session, so you can store the information in session or your own session handling code. Once the user logs into the generic site, you can theme it. You also have the option of storing a cookie, should the user allow, and allow the login to be themed.

When the vendor started trying to figure out co-branding, asking questions would have saved from this overly complex kludge I am dealing with. This leads us to lesson 2.

Refactor Early and Often

I have known this for years, but never thought to apply it to legacy code. Stupid me!

When I first found the kludge, we were under the gun, so I excused myself from fixing it then. That was a big mistake. In fact, I will state it is always a big mistake to ignore code problems and quality issues. If you can improve the code, take the time to do it now.

Now, you probably think this is impractical, and I agree. It certainly can be in some instances. If you have 200 baby refactoring steps to get the code 100% refactored, you might allow some give and take here (although the Agile purists are probably screaming right now). But, you cannot allow code with horrible kludges to go to production. I knew this at the time, but did not take the time as I felt I did not have the time.  Now it is biting me.

A couple of things that should be refactored out NOW:

  • Kludges in code where there is a clearly defined, sane alternativeExample: Pulling from external files versus themes
  • Bad practices in .NET or coding legacy style – Example: looping versus binding to display data. It is very ASP, not ASP.NET and it creates slow, kludgy code.

    for(int i=0;i<ds.Table1.Rows.Count;i++)
          output += "<tr><td>" + ds.Table1.Rows[i]["Field1"] + "</td>";
          output += … more <td>s here
          output += "</tr>";

    Yes, this code also has a problem with concatenating strings, which should also be immediately refactored when caught.

  • Duplicate code – this is the primary code smell. If you have code in more than one place, you will get burned some day. Not you “might”, but you “will”.

I am sure there are others that can be named and an argument could be made that all code smells should be removed prior to going on. You have to gauge how seriously you wish to take this, but at least get rid of any obvious insanity. Yes, it takes a bit of time, but it will waste far much more time later when you have to relearn the code to fix it. Depending on the complexity of the code, this could be hours, or even days , of lost productivity, while doing it today will only burn an hour or two.


I will close with this thought.

If you can’t find the time to do it right, when will you ever find the time to do it over?

Peace and Grace,


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: