Exploring Resharper
Deepak and Pete take one look at Pete's desk and decide to work at Deepak' s desk instead. They load up the CourseCosts library.
Here's a
version of the library that includes all of the code examined in this article.
You can download a trial version of Resharper from JetBrains at http://www.jetbrains.com/resharper/.
Deepak: I've already installed Resharper on this machine.
Pete: I guessed that is why you have the extra Resharper menu on your Visual Studio menu bar.
Deepak: Sue is right, You can be a smart ass sometimes!
Pete grins but remains silent.
Deepak: So let's see what this project consists of.
Pete: The files we'd be interested in are probably the CourseCost and CostItems classes.
Deepak: Oh! You do know something about this project, don't you?
Pete: Like I said, kind of
Deepak: Hey, there is even a test class in here with a whole test in it!
Pete: Yes it uses that
text.xml file, I remember this. We have to copy that file to the root of our
C: drive to get the test to work.
Deepak: And what exactly is the reason for that?
Pete: The CourseCost class works out the cost of a course for each attendee using the figures in the XML file. Open it up and I'll explain it to you.
Deepak opens the text.xml file shown below.
<?xml version="1.0" encoding="utf-8" ?>
<Courses>
<London>
<Venue>3000</Venue>
<Catering>30</Catering>
<Equipment>2500</Equipment>
<Speaker>2000</Speaker>
<MaxAttendees>30</MaxAttendees>
</London>
</Courses>
Pete: So the idea is there will be an entry for each location the company runs courses. Here we have only created one for the test: London. Then the cost items are listed as children of that entry. The
MaxAttendees and
Catering are kind of special cases.
Deepak: How so?
Pete: Well, the other costs are fixed costs for the course, The
Catering is a cost per attendee. The
MaxAttendees is not a cost at all, but the maximum number of attendees that venue can cater for.
Deepak: OK, makes sense so far. Let's look at the code.
Deepak opens the CourseCost.cs file as shown in Listing 1.
Deepak: Yes, this is what I was talking about! This is a real mess.
Pete: PANIC!
Eddie: Is everything OK with you guys?
Deepak: Yeah we're just looking at the messy code and
Pete: PANIC!
Deepak: I'm sure we can fix it!
Pete: (Sounding a bit doubtful.) OK, show me what we can do.
Deepak: OK, let's start with those public member variables. Do they really need to be public?
Pete: What, the
costItems and the
attendees?
Deepak: Yes, Resharper can show us where they are being used.
Pete: That's cool! How do we do that?
Deepak: Move the mouse over the
costItems variable name and right-click. Do you see the "Find Usages" item in the popup menu?
Pete: Yep.
Deepak: Click on it.
Pete does this and a dialog appears showing the places the variable is used (see
Figure 1).
 | |
| Figure 1. Find Usages Dialog: The figure shows the results of using the Find Usages dialog to find all occurrences of a variable. |
Deepak: As I thought, it is not used outside of this class. Let's change it to a private variable. Check the
attendees as well.
Pete: The same, I'll change both of them.
Deepak: Great, now let's look at that nasty big function
GetCost.
Pete: OK, what's wrong with it?
Deepak: I think it smells. It seems too large and is doing too much. We should take some of the functionality out into smaller methods.
Pete: OK, but don't we pay the cost of method invocation? Isn't that going to slow the code down?
Deepak: Is speed an issue for this component?
Pete: I don't know. It might be.
Deepak: I think until we know it is an issue, I'd rather not worry about it.
Pete: OK, What shall we do about this big method then?
Deepak: We can start by extracting the code that calculates the total cost into its own method. Select the
foreach loop that goes through the
costItems, and right-click. Do you see the Refactor menu item at the bottom of the popup menu?
 | |
| Figure 2. Extract Method: The figure shows the Extract Method dialog. |
Pete: Yes.
Deepak: Select that and then select Extract Method.
Pete does this and the Extract Method dialog box appears as shown in Figure 2.
Pete: OK, now what?
Deepak: How about changing the return value so it returns the
totalcost and we'll call the method
Pete: TotalCost?
Deepak: Yes, that should do it. You can see the signature preview at the bottom of the dialog. I like that! Click OK.
The dialog before Pete clicks OK is shown in Figure 3.
 | |
| Figure 3. Extract Method Dialog: The figure shows the Extract dialog used to extract the TotalCost method. |
Pete: Hey cool! It took the code out of the big function and made the call to it and everything!
Deepak: It's nice huh?
Pete: Sure is, what's next?
Deepak: Let's do the same with the other
foreach loop that extracts the cost items from the XML nodes.
Pete: OK, what shall we call this function?
Deepak: How about
ExtractCostItems?
Pete: Great, look at that! A few clicks of the mouse and the code is starting to look like proper object-oriented code!
Pete grins, guessing this is just the tip of the iceberg. The code that Pete and Deepak are now looking at is shown in Listing 2.
Eddie: How are you guys doing?
Pete: Really good. It's so easy to change the code using this Resharper tool.
Eddie: That's good to hear but are you thinking about why you are changing the code? What is the task at hand?
Deepak: Oh!
Pete: Um... we have just been playing with what's there. I haven't really been thinking about the task. Um
what is the task?
Deepak: Something to do with handling a sponsor paying for some of the costs.
Eddie: Chris, can you help these guys understand what the CourseCosts library does?