Go Back

Refactoring Part 4: Replacing temp variable with Query

Last time we moved a method, what I didn’t do was go through and find all the references and fix them to point to the new method. So we’ll do that now.

 

Basically it’s just one spot in this code, but in your apps you may have to ‘find all references’ which is actually a right click functionality in VS2008 (and vs2005, but vs.net seems to be flakey on it)

From:

    foreach (Rental rental in _rentals)

            {

                //determine amount for each line

                double thisAmount = AmountFor(rental);

To:

 

            foreach (Rental rental in _rentals)

            {

                //determine amount for each line

                double thisAmount = rental.Charge;

 

Then we can remove the old method

AmountFor(Rental rental)

 

Compile and verify everything works fine.

 

Optionally you could leave the method in if you’re worried about other apps using the public interface to this method.

 

 

Anyhow, now time for

 

Part 4: Replacing temp variable with Query

 

If you look at the statement method, you’ll see that thisAmount is redundant. Everywhere we see thisAmount we’re going to replace with the property from rental. If you’re worried about performance, Fowler urges us to refactor and that he’ll cover optimizing later on. Better to refactor first then optimize later he says. I haven’t finished the book but I’ll trust that’s the best thing to do at this point and move forward.

 

The new statement method will look like this:

 

public string Statement()

        {

            double totalAmount = 0;

            int frequentRenterPoints = 0;

            string result = "Rental Record for " + Name + Environment.NewLine;

            foreach (Rental rental in _rentals)

            {

                //add frequent renter points

                frequentRenterPoints++;

                //add bonus for two day release rental

                if ((rental.Movie.PriceCode == Movie.NEW_RELEASE) &&

                    rental.DaysRented > 1) frequentRenterPoints++;

 

                //show figures for this rental

                result += "\t" + rental.Movie.Title + "\t$" +

                    rental.Charge.ToString() + Environment.NewLine;

                totalAmount += rental.Charge;

 

            }

            //add footer lines

            result += "Amount owed is $" + totalAmount.ToString() + Environment.NewLine;

            result += "You earned " + frequentRenterPoints.ToString() + " frequent renter points";

            return result;

        }

 

.net Terminology note: I think that java developers call these getter methods "queries"... However, in .net we use properties. Not always is there a 1 to 1 translation but in general when you see the word Query used in fowler's book try to use a property to be more ".Net" friendly.

 

Also: if a getBlah() method requires parameters... you can't really make it a property in C#... you can in VB.Net though.

Facebook DZone It! Digg It! StumbleUpon Technorati Del.icio.us NewsVine Reddit Blinklist Furl it!

Post a comment!
  1. Formatting options