Monthly Archive for: ‘June, 2009’

Empty Catch Blocks

11

I’ve just found 22 examples of this anti-pattern in a small .NET project:

emptycatchblock

Oh yes, the empty catch block. Joy of joys.

Declaring an empty catch block is equivalent to professing that:

"Sometimes this code I’ve written fails. I don’t know why. I don’t really care. Please don’t tell me when it happens. I can’t hear you. La la la…"

*sigh*

If I ruled the world, empty catch blocks would be invalid in C#. Is there ever any reason for using them?

SQL 2008 – Missing Indexes Hints

2

Improvements to the Execution Plan UI in SQL Server Management Studio 2008 sure do make it a doddle to improve query execution time.

A client just reported that a particular stored procedure kept timing out.

Viewing the Estimated Execution Plan for this query displayed a nice green message advising of a potentially missing index which could improve performance by >95%:

amc1

Right-clicking and selecting “Missing Index Details” then brings up a new query window containing the index creation DDL (I had changed the index name by the time I grabbed this capture):

amc2

Dead easy. Repeating the process identified a second index which could further improve performance by >80%.

Of course, you still need to bear in mind other queries which access the table, and the effects on inserts/updates of creating additional indexes, but even so, this really helps the workflow.

The suggested indexes functionality was actually available in SSMS 2005 too, but the results were buried away in the XML version of the execution plan, and obscure properties windows, rather than being so in your face.

SRP Doesn’t Stand For “Several Responsibility Principle”

0

I know it should no longer come as any surprise to me, but I just found this method (and several others like it) lurking in a Data Access Object. Grrr…

/// <summary>
/// Send an email to notify that accrual data has been successfully uploaded
/// </summary>
/// <param name="accrualUpload"></param>
/// <param name="recipientAddresses"></param>
public void SendSuccessfulUploadEmail(AccrualUpload accrualUpload, Collection&lt;MailAddress&gt; recipientAddresses)

Much of software design / architecture isn’t technically difficult, it just requires a little thought – "where does this functionality belong?&quot; – rather than forcing code into the first class you come across.

Square Peg in a Round Hole

Did I mention how useful NDepend is in exposing idiocy like this? I think I did. And I really should get round to blogging about how handy CodeRush and Refactor Pro are for cleaning up such smells.

.NET – Retrieving the Month Name

1

Here’s another amusing method I recently found in some production code which comes straight from the “can’t be bothered to learn the framework (or how to Google)” style of programming:


/// <summary>
/// Converts a month number into the name
/// </summary>
/// <param name="monthNumber"></param>
private string GetMonthName(int monthNumber)
{
return String.Format("{0:MMMM}", new DateTime(1969, monthNumber, 1));
}

I wonder what the significance of 1969 is? Year of the author’s birth, perhaps?!

Aside from being hacky, there is a performance implication to unnecessarily creating new DateTime objects and performing string formatting in this way. In my tests, I’ve found that this method is around 18 times slower than simply calling System.Globalization.DateTimeFormatInfo.CurrentInfo.GetMonthName(monthNumber).

Ian Nullson

0

Oh dear. Royal Mail have been taking tips from Creative:

RoyalMail

Agile Yorkshire @ The Pub – Gojko Adzic, 10th June

0

If you’re in the Leeds area next Wednesday, why not come along to this Agile Yorkshire session?

10th June – Gojko Adzic: Slim, The Future Of FitNesse

Details

This talk will introduce Slim, the most important upgrade to FitNesse in years. Slim is the new test runner which promises to bring platform interoperability, easier integration, a much simpler programming model and lots of small helpers that will allow us to write and maintain executable specifications and acceptance tests easier. This is a session for .NET and Java developers. Some prior exposure to FitNesse and FIT would be beneficial, but not required.

Speaker

Gojko Adzic is a software craftsman with a passion for new technologies, programming and writing. He runs Neuri Ltd, a UK-based consultancy that helps companies build better software by introducing agile practices and tools and improving communication between software teams, stakeholders and clients. See http://gojko.net/about/ for more details.

Venue: Victoria Hotel, Central Leeds

Date/Time: Wednesday, 10th June, 6:30pm sharp.

More details: http://www.agileyorkshire.org/