Docsity

Refactoring - Slides - Programming Language Technologies and Paradigms | CMSC 433, Study notes of Programming Languages

Partial preview of the text

Download Refactoring - Slides - Programming Language Technologies and Paradigms | CMSC 433 and more Study notes Programming Languages in PDF only on Docsity!

CMPUT 301: Lecture 14

Refactoring

Lecturer: Sherif Ghali

Department of Computing Science

University of Alberta

Notes credits:

Ken Wong, Sherif Ghali

(Some slides removed by M. Hicks)

2

Refactoring

Example:

a program to calculate and print a

statement of a customer s charges at a

video store

3

Class Diagram

-priceCode : int
Movie
Rental
-daysRented : int
Customer
+statement()
+getDaysRented (): int
+getPriceCode (): int

4

Movie

public class Movie {
public static final int CHILDRENS = 2;
public static final int REGULAR = 0;
public static final int NEW_RELEASE = 1;
private String _title;
private int _priceCode;
public Movie( String title, int priceCode ) {
_title = title;
_priceCode = priceCode;
public int getPriceCode() {
return _priceCode;
public void setPriceCode( int arg ) {
_priceCode = arg;
public String getTitle() {
return _title;

7

Customer::statement()

public String statement() { double totalAmount = 0; int frequentRenterPoints = 0; Enumeration rentals = _rentals.elements(); String result = Rental Record for + getName() + \n ;

while (rentals.hasMoreElements()) { double thisAmount = 0; Rental each = (Rental)rentals.nextElement();

// determine amounts for each line switch (each.getMovie().getPriceCode()) { case Movie.REGULAR: thisAmount += 2; if (each.getDaysRented() > 2) thisAmount += (each.getDaysRented() - 2) * 1.5; break; case Movie.NEW_RELEASE: thisAmount += each.getDaysRented() * 3; break; case Movie.CHILDRENS: thisAmount += 1.5; if (each.getDaysRented() > 3) thisAmount += (each.getDaysRented() - 3) * 1.5; break; }

8

Customer::statement()

// add frequent renter points frequentRenterPoints++; // add bonus for a two day new release rental if ((each.getMovie().getPriceCode() == Movie.NEW_RELEASE) && each.getDaysRented() > 1) frequentRenterPoints++;

// show figures for this rental result += \t + each.getMovie().getTitle() + \t + String.valueOf( thisAmount ) + \n ; totalAmount += thisAmount; }

// add footer lines result += Amount owed is + String.valueOf( totalAmount ) + \n ; result += You earned + String.valueOf( frequentRenterPoints ) + frequent renter points ; return result; } }

9

Refactoring

:Customer :Rental :Movie
* [for all rentals]
getMovie ()
getPriceCode ()
getDaysRented ()
statement()

10

Refactoring

Something is rotten in the state of

Denmark

What is it?

13

Refactoring

First step:

Build self-checking tests.

14

Refactoring

Decompose statement() method:

Extract logical chunk of code as a new

method.

Apply Extract Method.

15

class Customer {

public String statement() { double totalAmount = 0; int frequentRenterPoints = 0; Enumeration rentals = _rentals.elements(); String result = Rental Record for + getName() + \n ;

while (rentals.hasMoreElements()) { double thisAmount = 0; Rental each = (Rental)rentals.nextElement();

// determine amounts for each line switch (each.getMovie().getPriceCode()) { case Movie.REGULAR: thisAmount += 2; if (each.getDaysRented() > 2) thisAmount += (each.getDaysRented() - 2) * 1.5; break; case Movie.NEW_RELEASE: thisAmount += each.getDaysRented() * 3; break; case Movie.CHILDRENS: thisAmount += 1.5; if (each.getDaysRented() > 3) thisAmount += (each.getDaysRented() - 3) * 1.5; break; }

16

Refactoring

class Customer {

public String statement() { double totalAmount = 0; int frequentRenterPoints = 0; Enumeration rentals = _rentals.elements(); String result = Rental Record for + getName() + \n ;

while (rentals.hasMoreElements()) { double thisAmount = 0; Rental each = (Rental)rentals.nextElement();

thisAmount = amountFor( each );

19

Refactoring

Rename variables in amountFor():

Enhance readability.

20

Refactoring

class Customer {

private double amountFor( Rental each ) { double thisAmount = 0; switch (each.getMovie().getPriceCode()) { case Movie.REGULAR: thisAmount += 2; if (each.getDaysRented() > 2) thisAmount += (each.getDaysRented() - 2) * 1.5; break; case Movie.NEW_RELEASE: thisAmount += each.getDaysRented() * 3; break; case Movie.CHILDRENS: thisAmount += 1.5; if (each.getDaysRented() > 3) thisAmount += (each.getDaysRented() - 3) * 1.5; break; } return thisAmount; }

}

21

Refactoring

class Customer {

private double amountFor( Rental aRental ) { double result = 0; switch (aRental.getMovie().getPriceCode()) { case Movie.REGULAR: result += 2; if (aRental.getDaysRented() > 2) result += (aRental.getDaysRented() - 2) * 1.5; break; case Movie.NEW_RELEASE: result += aRental.getDaysRented() * 3; break; case Movie.CHILDRENS: result += 1.5; if (aRental.getDaysRented() > 3) result += (aRental.getDaysRented() - 3) * 1.5; break; } return result; }

}

22

Refactoring

Compile and test.

What do we do next?

25

Refactoring

class Rental {

double getCharge() { double result = 0; switch (getMovie().getPriceCode()) { case Movie.REGULAR: result += 2; if (getDaysRented() > 2) result += (getDaysRented() - 2) * 1.5; break; case Movie.NEW_RELEASE: result += getDaysRented() * 3; break; case Movie.CHILDRENS: result += 1.5; if (getDaysRented() > 3) result += (getDaysRented() - 3) * 1.5; break; } return result; }

}

26

Refactoring

class Customer {

private double amountFor( Rental aRental ) { return aRental.getCharge(); }

}

27

Refactoring

Compile and test.

28

Refactoring

Replace references to amountFor()

with getCharge():

Adjust references to old method to use

new method.

Remove old method.

31

Refactoring

Compile and test.

32

Refactoring

Eliminate thisAmount temporary in

statement():

Replace redundant temporary variable with

query.

Apply Replace Temp with Query.

33

Refactoring

class Customer {

public String statement() { double totalAmount = 0; int frequentRenterPoints = 0; Enumeration rentals = _rentals.elements(); String result = Rental Record for + getName() + \n ;

while (rentals.hasMoreElements()) { double thisAmount = 0; Rental each = (Rental)rentals.nextElement();

thisAmount = each.getCharge();

// add frequent renter points frequentRenterPoints++; // add bonus for a two day new release rental if ((each.getMovie().getPriceCode() == Movie.NEW_RELEASE) && each.getDaysRented() > 1) frequentRenterPoints++;

// show figures for this rental result += \t + each.getMovie().getTitle() + \t + String.valueOf( thisAmount ) + \n ; totalAmount += thisAmount; }

34

Refactoring

class Customer {

public String statement() { double totalAmount = 0; int frequentRenterPoints = 0; Enumeration rentals = _rentals.elements(); String result = Rental Record for + getName() + \n ;

while (rentals.hasMoreElements()) { Rental each = (Rental)rentals.nextElement();

// add frequent renter points frequentRenterPoints++; // add bonus for a two day new release rental if ((each.getMovie().getPriceCode() == Movie.NEW_RELEASE) && each.getDaysRented() > 1) frequentRenterPoints++;

// show figures for this rental result += \t + each.getMovie().getTitle() + \t + String.valueOf( each.getCharge() ) + \n ; totalAmount += each.getCharge(); }

37

Refactoring

class Customer {

public String statement() { double totalAmount = 0; int frequentRenterPoints = 0; Enumeration rentals = _rentals.elements(); String result = Rental Record for + getName() + \n ;

while (rentals.hasMoreElements()) { Rental each = (Rental)rentals.nextElement();

// add frequent renter points frequentRenterPoints += each.getFrequentRenterPoints();

// show figures for this rental result += \t + each.getMovie().getTitle() + \t + String.valueOf( each.getCharge() ) + \n ; totalAmount += each.getCharge(); }

38

Refactoring

class Rental {

int getFrequentRenterPoints() { if ((getMovie().getPriceCode() == Movie.NEW_RELEASE) && getDaysRented() > 1) return 2; else return 1; }

}

39

Refactoring

Eliminate totalAmount temporary:

Apply Replace Temp with Query.

40

Refactoring

class Customer {

public String statement() { double totalAmount = 0; int frequentRenterPoints = 0; Enumeration rentals = _rentals.elements(); String result = Rental Record for + getName() + \n ;

while (rentals.hasMoreElements()) { Rental each = (Rental)rentals.nextElement();

// add frequent renter points frequentRenterPoints += each.getFrequentRenterPoints();

// show figures for this rental result += \t + each.getMovie().getTitle() + \t + String.valueOf( each.getCharge() ) + \n ; totalAmount += each.getCharge(); }

// add footer lines result += Amount owed is + String.valueOf( totalAmount ) + \n ; result += You earned + String.valueOf( frequentRenterPoints ) + frequent renter points ; return result; } }