A Day Spent Refactoring Code

I spent a good part of today refactoring some code I’ve inherited. It’s a VB.Net wrapper around some commercial address verification software. Two examples of the type of functionality the software provides are address correction and address formatting.

Here is a simplified version of three of the classes I was given to work with: Address, CorrectedAddress and FormattedAddress. An Address is what you would provide to the verification software and the other two classes would be returned from either the address correction or address formatting functionality.

Public Class Address
    Public AddressLine As String
    Public City As String
    Public Province As String
    Public PostalCode As String
    Public Country As String
End Class
Public Class CorrectedAddress : Inherits Address
    Public Status As CorrectedAddress.Status
    Public Message As String

    Public Enum Status
        Valid
        Invalid
        Corrected
        [Error]
    End Enum
End Class
Public Class FormattedAddress
    Public AddressLineOne As String
    Public AddressLineTwo As String
    Public AddressLineThree As String

    Public Status As FormattedAddress.Status
    Public Message As String

    Public Enum Status
        Valid
        Invalid
        [Error]
    End Enum
End Class

Note that CorrectedAddress inherits from Address, but FormattedAddress does not. Both CorrectedAddress and FormattedAddress have a Status and Message properties (implemented as public fields above only for brevity). The Status properties both return similar, but not identical enumerations. In the actual code there were six classes like these two, but I am focusing only on these two for simplicity.

The whole thing didn’t seem quite right to me, and I struggled a bit to put my finger on how this should have been implemented. But then it hit me. CorrectedAddress isn’t really an Address. It’s a result of an address correction (that happens to have an address). “Is A” versus “Has A”. Remember the oft quoted Object-Oriented design principle:

Favour Composition over Inheritance

So I pulled the Address class out of the inheritance hierarchy, renamed CorrectedAddress and FormattedAddress to CorrectedResult and FormattedResult respectively and even threw in some Generics stuff to deal with the similar but different Status enumerations.

The result (the Address class was left as-is):

Public MustInherit Class Result(Of T As Structure)
    Public Status As T
    Public Message As String
End Class
Public Class CorrectedResult : Inherits Result(Of CorrectedResultStatus)
    Public Address As Address

    Public Enum CorrectedResultStatus
        Valid
        Invalid
        Corrected
        [Error]
    End Enum
End Class
Public Class FormattedResult : Inherits Result(Of FormattedResultStatus
    Public AddressLineOne As String
    Public AddressLineTwo As String
    Public AddressLineThree As String

    Public Enum FormattedResultStatus
        Valid
        Invalid
        [Error]
    End Enum
End Class

Much better!

If Anyone is Wondering Why…

… I’ve written more blog posts for this month than I have for the previous, oh maybe, three years combined, it’s because I’ve been using Windows Live Writer to manage my blog posts (and pages). It’s a wicked tool that makes it super easy to manage blog content. In my experience so far it has flawlessly handled posting and updating my WordPress based site.

Upgrading a WordPress Site Using Subversion

image

A few weeks ago I finally made the upgrade jump from WordPress version 2.5.1 to version 2.6.2. I skipped the versions in between because while I haven’t got a lot of experience upgrading WordPress, from what experience I have had it has usually been a pain. Especially if I’ve done any kind of customization (which I have).

So when I did make the jump to 2.6.2 I resolved to do it in such a way that any future upgrades would be easy. So what’s the obvious solution to the problem of controlling versions? Version control, of course! I am a big Subversion user and luckily the creators of WordPress are Subversion users too!

Continue reading

AnkhSVN is AWESOME!

What is AnkhSVN? It’s a Subversion source control provider for Visual Studio. It allows you to use Subversion for version control in Visual Studio rather than something like Visual SourceSafe. It’s also open-source and free.

I can just hear some people saying “I’ve tried AnkhSVN. It sucks. VisualSVN is the closest you can get to working Subversion / Visual Studio integration”. I can even hear a few people saying “They both suck. Who needs IDE integration anyway. TortoiseSVN from Windows Explorer is just fine”.

I have shared both of these views at various points in the last few years. But today I discovered and tried AnkhSVN 2.0! Huge difference! The new version is a nearly complete rewrite. It’s no longer an add-in, it’s own a proper SCC provider.

Do yourself a favour and give it a try before you drop any money on VisualSVN.

I Survived a Busy Long Weekend

Went up to Quebec City with the family this past weekend. The wife and I both have family up there so we try to go up at least once a year (this was our second trip this year).

We leave Friday night after work and drive as far as Trois-Rivieres, which after a full day of work and several hours in the car with the little one is far enough. We crash overnight in a hotel, then cover the last couple hours to Quebec City on Saturday morning. We spend most of Saturday with my family, then Saturday night we crash at the in-laws place. Sunday is time spent with the wife’s family, and some touristy activities. Then Monday we head back home. This time we stopped in Montreal and spent a few hours at the Bio-Dome. It’s a pretty cool place, but a little on the expensive side.

We got home Monday night and did nothing but unpack and watch a little TV. I spent last night and most of tonight getting caught up on 5 days worth of emails (approximately 1600, 75% spam) and my Google Reader feeds (1000+ articles). I’ve still got 900+ unread items in Google Reader.

First Impressions: Rogers Video Direct (aka Zip.ca)

I’ve been a subscriber of Rogers Video Direct (a Rogers branded version of Zip.ca) for about a month now and so far it’s working out for me.

Basically how it works is that you sign up for an account, choose a plan which dictates how many DVDs you can have out at once and how many you can have shipped to you for free each month. I started out with 3 at a time, unlimited per month, but have switched to one at a time, unlimited per month after getting a feel for exactly how long it actually takes me to watch a DVD given my schedule. Even on the 1 at a time plan there is a ‘Refill’ feature which you can use to indicate that the DVD you’re returning is in the mail. They’ll basically send out your next DVD before before they receive the last one. In my experience I receive my next DVD the same day that they receive to previous DVD return.

Anyways, once you’ve signed up, you browse their selection of DVDs and add the ones you’re interested in to you ZipList. You can rank the DVDs on your list and what they do is send you the highest ranked DVD on your list that is available.

I am currently on my 4th DVD. I find the DVDs in my mailbox within 2 days of receiving notification that they’ve shipped and the return trip seems to take about the same time (2 days after I mail the DVD back it disappears from my current rented DVD list.

I’ve got years of movies to get caught-up with!