Another Nice DevExpress CodeRush Refactoring

For the last few days, I’ve been using DevExpress CodeRush and am finding some very useful refactorings.  Many I’m not blogging about, but there are a few that I really like.  In this post, I’m going to show just two of those refactorings that have been making my code much nicer and easier to write.  One is the “Introduce Using” refactoring, and the other is “Convert to Lambda Expression”.

Before I go into the details, I’d just like to disclose that when I was first writing the Silicon Valley Code Camp web site, I was an asp.net and c# newby.  I’m not claiming wizard status now, but I have to admit that when I go back and look at some of the code I wrote back then (including what I’m showing below before the refactoring), it’s a little embarrassing.  Silicon Valley Code Camp for me as “when I’m not doing real work” web site so I don’t really have the time to go back and clean things up.  Now, with CodeRush, it’s easy to clean things up when I see them with very little effort.


Introduce Using Statement Refactoring

Here is the code I wrote 5+ years ago:

SqlConnection conn = new SqlConnection(connectionString);
conn.Open();

// First, make a quick list of counts for each id that is unavailable and show
// not be shown
string selectCount = @"select VistaSlotsId FROM attendees
WHERE VistaSlotsId >= 2 AND VistaSlotsId <= 5 Group By VistaSlotsId
HAVING COUNT(*) >= @MaxPerSlot "
;
List<int> unavailableSlotsList = new List<int>();
SqlDataReader readerUnavailable = null;
SqlCommand cmdUnAvailable = new SqlCommand(selectCount, conn);
cmdUnAvailable.Parameters.Add("@MaxPerSlot", SqlDbType.Int).Value = maxPerSlot;
readerUnavailable = cmdUnAvailable.ExecuteReader();
try
{
while (readerUnavailable.Read())
{
int id = readerUnavailable.GetInt32(0);
unavailableSlotsList.Add(id);
}
}
finally
{
if (readerUnavailable != null) readerUnavailable.Close();
}

By placing the cursor over the “conn” on the top line of the code above, CodeRush gives us the following result:

image

There are actually quite a few things happening here.

  1. It is showing us in the top textbox what the using syntax will be
  2. It is crossing out the lines of code it will change
  3. It is teaching us a little about the using statement

If I accept the changes, I get a nice refactoring.  I can continue doing this with “using” for SqlCommand and SqlReader.  I did need to move the SqlReader declaration inside the SqlCommand codeblock for this to work.  Here is the final refactored code that took about 10 seconds.  By hand, I’d say it would have taken me 3 minutes and I may have gotten it wrong which is why I would never do it before.  Now, I’m confident I did not break my code.

using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
// First, make a quick list of counts for each id that is unavailable and show
// not be shown
string selectCount = @"select VistaSlotsId FROM attendees
WHERE VistaSlotsId >= 2 AND VistaSlotsId <= 5 Group By VistaSlotsId
HAVING COUNT(*) >= @MaxPerSlot "
;
List<int> unavailableSlotsList = new List<int>();
using (SqlCommand cmdUnAvailable = new SqlCommand(selectCount, conn))
{
cmdUnAvailable.Parameters.Add("@MaxPerSlot", SqlDbType.Int).Value = maxPerSlot;
using (SqlDataReader readerUnavailable = cmdUnAvailable.ExecuteReader())
{
try
{
while (readerUnavailable.Read())
{
int id = readerUnavailable.GetInt32(0);
unavailableSlotsList.Add(id);
}
}
finally
{
if (readerUnavailable != null)
readerUnavailable.Close();
}
}
}

 

Compress To Lambda Expression Refactoring

A lot of the Silicon Valley Code Camp web site was written prior to the introduction of Lamba Expressions.  If you recall, I wrote an MSDN article back in 2006 entitled Adding Personalization via Profiles to the ObjectDataSource in ASP.NET 2.0.  Ted Neward inspired a construct to cleverly sort the result list using delegates as shown in the screen shot (form that above post).

image

 

The code below is similar to the above but not quite the same.  This is how it looks before the refactoring:

Comparison<DataObjectSessionsOverview> comparison = null;
switch (sortDataBase)
{
case "Userfirstname":
comparison = new Comparison<DataObjectSessionsOverview>(
delegate(DataObjectSessionsOverview lhs, DataObjectSessionsOverview rhs)
{
return lhs.Userfirstname.CompareTo(rhs.Userfirstname);
}
);
break;

CodeRush suggests:

image

Which then give us:

switch (sortDataBase)
{
case "Userfirstname":
comparison = (lhs, rhs) => lhs.Userfirstname.CompareTo(rhs.Userfirstname);
break;

I like it!

About Peter Kellner

Peter is a software professional specializing in mobile and web technologies. He has also been a Microsoft MVP for the past 7 years. To read more about Peter Kellner and his experience click here. For information about how Peter Kellner might be able to help you with your project click here.

Follow me:


Comments

  1. Greetings I recently finished reading through through your blog as well as I’m very impressed. I truly do have a couple inquiries for you personally however. You think you’re thinking about doing a follow-up submitting about this? Will you be gonna keep bringing up-to-date also?

  2. Hi Peter Petrov,

    Totally agree, the try/finally should be removed. Not sure it’s reasonable to expect that a tool could have figured that out.

  3. No need of the try/finally. The reader will be closed(disposed) by the using statement. The code will be more readable.

  4. I use both products and am very happy doing that.

  5. Hi Peter,
    I am not familiar with CodeRush, but from what I see in your post, its functionality seems to be similar to ReSharper, which you have written about in the past. This got me curious: are you using both, or did you switch to CodeRush?
    Cheers (and happy 4th of July!)
    Mathias

Follow

Get every new post delivered to your Inbox

Join other followers: