Go Back

Refactoring Part 6: Moving Temps to Properties

Refactoring continues! Tonight I’m working on the next stage, which is some more removing of temp variables and replacing them with queries. Again, in the book Fowler says, refactor temps out of your methods… later we’ll work on performance issues! (I’m still waiting to see where he does this but he is the master and I am the apprentice.)

 

Basically, we have the Statement() Method inside Customer that looks 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 += rental.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;

        }

 

We’re going to replace the totalAmount and frequentRenterPoints temp variables and move them to Queries of their own. In our case with .net we’re creating our Query methods as properties where possible.

 

The result is this:

        public string Statement()

        {

           

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

            foreach (Rental rental in _rentals)

            {

                //show figures for this rental

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

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

            }

            //add footer lines

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

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

            return result;

        }

        private int FrequentRenterPoints

        {

            get

            {

                int result = 0;

                foreach (Rental rental in this._rentals)

                {

                    result += rental.FrequentRenterPoints;

                }

                return result;

            }

        }

        private double TotalCharge

        {

            get

            {

                double result = 0.0;

                foreach (Rental rental in this._rentals)

                {

                    result += rental.Charge;

                }

                return result;

            }

        }

 

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

Post a comment!
  1. Formatting options