www.codinghillbilly.com   kyle.baley.org  Subscribe / Contact
 
 
 
 
LATEST POSTS
Wednesday, December 12, 2007

Greetings brothers and sisters and solidarity to you. The revolution is off to a tremendous 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:

///.*\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: \t*\#region.*\n and \t*\#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

Wednesday, December 12, 2007 10:52:40 AM (Eastern Standard Time, UTC-05:00)
Another thing to mention is that doing a "Throw ex" kills off the stack trace. The proper way (as you show here) is to just do a "Throw". Detailed here

As you point out, it is rare you should actually need to catch/throw exception in code anyways.
Wednesday, December 12, 2007 11:37:11 AM (Eastern Standard Time, UTC-05:00)
Actually, the proper way for these particular examples is to remove the catch completely as it adds no discernible (there's that word again!) value. And as you point out, it is actually harmful if you simply rethrow the exception you've caught.

Luckily ReSharper knows both of these problems. In the first case, it offers to remove the redundant catch completely. In the second, it will refactor from throw ex; to throw; and then you need to use the "remove redundant catch" refactoring again as in the first example.

James Kovacs has a good post on it here.
Kyle
Comments are closed.

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Copyright © 2008 Kyle Baley. All rights reserved.
 
LATEST POSTS
 
POPULAR POSTS
 
LINKS
 
BLOG ROLL
 
CATEGORIES
 
ARCHIVE