Skip to content

Another Nice DevExpress CodeRush Refactoring

Updated: at 05:05 PM

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!