Sponsored By Aspose - File Format APIs for .NET

Aspose are the market leader of .NET APIs for file business formats – natively work with DOCX, XLSX, PPT, PDF, MSG, MPP, images formats and many more!

Refactoring by rote, or "How to have a ReSharper Orgy!"

Greetings brothers and sisters and solidarity to you. The revolution is off to a rousing start. We have integrated continuously and Comrade Marc Davis has won a key early victory by excising the putrid web service that so fiendishly guarded our data access layer.

As for myself, I have just returned from a bloody battle with the enemy and have come away battered but triumphant. It was a great blow to the despots of uncohesiveness!

Our wily foes tried to gain the upper hand early by striking when our defenses were low. After opening a seemingly benign file, we were awash, AWASH I tell you!, in a veritable SEA OF ORANGE running along the right side.

I stared dumbfounded at the awesomeness of the task before me. "How can this be?" asks I, "this is no mere autogenerated class? This class was typed by hand. Clearly by the devil himself!" No matter, with a cry of "ONWARD!!! TO BE RELEASED FROM UNDER THE OPPRESSIVE THUMBS OF THE HIGHLY-COUPLED SWINE-PIGS!!" I leap headlong into the fray.

My battle begins and I become the Comment Warrior. It is a bold move because of the sheer numbers. <summary> tags that hold no relevance, <param> tags for non-existent parameters. No comment survives. Void methods have been tagged with <returns>Nothing</returns>, a move which stuns me at first but I quickly recover. After several skirmishes, I no longer notice the empty lines of History: in the <remarks> tags as I go about decapitating the methods in the name of maintainability.

Soon, I see an opening! Find & Replace can take a regular expression! After a brief two-day refresher of the syntax, the first phase is mine in one fell swoop of replacing the following expression with an empty string:

^:b*///.*\n

With that, the opposing army is almost 1000 less strong. But the enemy is quick and responds with waves upon waves upon waves of:

    catch
    {
        throw;
    }

along with their slightly bigger pawns*:

    catch ( Exception ex )
    {
        throw ex;
    }

These blocks are thrown at me from *every single method*. Some even have finally calls that set object references to null. Clearly, this army’s overlords were trained in the style of Crouching Tiger/Visual Basic from the late 20th century.

Luckily, they are easily picked up with the Blade of Alt-Enter from my ReSharper arsenal. But after untold hours, my fingers become weary. And my Mace of Shift-Ctrl-Alt-F fails me when it is unable to reformat the 6000+ soldiers remaining in this class. But I risk a glance to the right and allow myself a brief bout of euphoria as the orange gives way to the dull gray of victory.

By this time, the battle is all but won. I drive a final blow by deleting all regions (regular expressions: ^:b*\#region.*\n and ^:b*\#endregion.*\n) and with a flourish of Ctrl+Alt+O, all references are properly qualified for the final victory!

Resistance is NOT futile!

Kyle the Understated

* Comrades Dave Woods and James Kovacs both have posts on why this variation is particularly heinous

This entry was posted in Refactoring. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://codebetter.com/blogs/kyle.baley Kyle Baley

    @mike61079: Yeah, those were the days, weren’t they?

    But I don’t want to disappoint so I’ve distilled this post down to the Jack Webb version:
    – Comments are useless if they aren’t accurate
    – You can use regular expressions in Find & Replace in Visual Studio
    – The regular expression ^:b*///.*\n matches triple-slash comments
    – If you’re not going to do anything with your exception, catching and throwing it is redundant. And watch you don’t swallow the stack trace
    – Ctrl+Shift+Alt+F (to reformat code in ReSharper) doesn’t work on classes of 6000+ lines
    – The regular expressions ^:b*\#region.*\n and ^:b*\#endregion.*\n match #region and #endregion tags respectively

    Hmmm, that was a *lot* easier to write than my original version above. Maybe I’ll stick with this format from now on….

  • mike61079

    I remember when I use to learn something from these blogs.

  • Brendan Tompkins

    I usually leave the sea of orange, let sleeping dogs lie as they say. I’m thinking that now I may paint my face blue, don my kilt, take off my underwear and release the battle cry!

    You’re inspiring!

  • Sergio Pereira

    Wow, you’ve just killed the lemmings with a BFG 9000