Refactoring Lecture 7 January 02, 2009 O b j e c t O r i e n t e - - PowerPoint PPT Presentation

refactoring
SMART_READER_LITE
LIVE PREVIEW

Refactoring Lecture 7 January 02, 2009 O b j e c t O r i e n t e - - PowerPoint PPT Presentation

366 Refactoring Lecture 7 January 02, 2009 O b j e c t O r i e n t e d S o f t w a r e E n g i n e e r i n g Refactoring: problem statement O b j e c t O r i e n t e d S o f t w a r e E n g i n e e r i n g Add a htmlStatment method


slide-1
SLIDE 1

January 02, 2009

O b j e c t O r i e n t e d S o f t w a r e E n g i n e e r i n g

366

Refactoring

Lecture 7

slide-2
SLIDE 2
slide-3
SLIDE 3
slide-4
SLIDE 4
slide-5
SLIDE 5
slide-6
SLIDE 6

January 02, 2009

O b j e c t O r i e n t e d S o f t w a r e E n g i n e e r i n g

377

Refactoring: problem statement

Add a htmlStatment method which returns a customer statement string containing html tags. ...and there will be some changes to the way movies are classified ...affecting frequent renter points and charging.

slide-7
SLIDE 7
slide-8
SLIDE 8

January 02, 2009

O b j e c t O r i e n t e d S o f t w a r e E n g i n e e r i n g

380

Refactoring: step 2

public String statement() { double totalAmount = 0; int frequentRenterPoints = 0; Enumeration rentals = _rental.elements(); String result = ÒRental Record for Ò + getName() + Ò\nÓ; while (rentals.hasMoreElements()) { double thisAmount = 0; Rental each = (Rental) rentals.nextElement(); thisAmount = amountFor(each); frequentRenterPoints ++; if ((each.getMovie().getPriceCode() == Movie.NEW_RELEASE)&& each.getDaysRented() > 1) frequentRenterPoints++; result += Ò\tÓ + each.getMovie().getTitle()+ Ò\tÓ + String.valueOf(thisAmount) + Ò\nÓ; totalAmount += thisAmount; } result += ÒAmount owed is Ò+Sting.valueOf(totalAmount) + Ò\nÓ; result += ÒYou earned Ò+Sting.valueOf(frequentRenterPoints) + Òfrequent renter points\nÓ; return result;

slide-9
SLIDE 9
slide-10
SLIDE 10
slide-11
SLIDE 11
slide-12
SLIDE 12

January 02, 2009

O b j e c t O r i e n t e d S o f t w a r e E n g i n e e r i n g

389

Refactoring: step 7

class Customer ... public String statement() { double totalAmount = 0; int frequentRenterPoints = 0; Enumeration rentals = _rental.elements(); String result = ÒRental Record for Ò + getName() + Ò\nÓ; while (rentals.hasMoreElements()) { double thisAmount = 0; Rental each = (Rental) rentals.nextElement(); thisAmount = amountFor(each); // 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; É

slide-13
SLIDE 13

January 02, 2009

O b j e c t O r i e n t e d S o f t w a r e E n g i n e e r i n g

390

Refactoring: step 7

class Customer ... public String statement() { double totalAmount = 0; int frequentRenterPoints = 0; Enumeration rentals = _rental.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;

slide-14
SLIDE 14
slide-15
SLIDE 15
slide-16
SLIDE 16
slide-17
SLIDE 17
slide-18
SLIDE 18
slide-19
SLIDE 19
slide-20
SLIDE 20
slide-21
SLIDE 21
slide-22
SLIDE 22
slide-23
SLIDE 23
slide-24
SLIDE 24
slide-25
SLIDE 25
slide-26
SLIDE 26
slide-27
SLIDE 27
slide-28
SLIDE 28

January 02, 2009

O b j e c t O r i e n t e d S o f t w a r e E n g i n e e r i n g

420

Refactoring: step 14

abstract class Price { abstract int getPriceCode(); } class ChildrenPrice extends Price { int getPriceCode(){ return MOVIE.CHILDREN; } } class NewReleasePrice extends Price { int getPriceCode(){ return MOVIE.NEW_RELEASE; } } class RegularPrice extends Price { int getPriceCode(){ return MOVIE.REGULAR; } }

slide-29
SLIDE 29
slide-30
SLIDE 30
slide-31
SLIDE 31
slide-32
SLIDE 32
slide-33
SLIDE 33
slide-34
SLIDE 34
slide-35
SLIDE 35
slide-36
SLIDE 36
slide-37
SLIDE 37
slide-38
SLIDE 38
slide-39
SLIDE 39
slide-40
SLIDE 40
slide-41
SLIDE 41

January 02, 2009

O b j e c t O r i e n t e d S o f t w a r e E n g i n e e r i n g

447

Bad Smells in Code

Comments (stench 2)

Comments are often a sign of unclear code... consider refactoring