Docsity

Composing Methods, Moving Features and Organization Data - Refactoring | CMPSCI 220, Study notes of Computer Science

Partial preview of the text

Download Composing Methods, Moving Features and Organization Data - Refactoring | CMPSCI 220 and more Study notes Computer Science in PDF only on Docsity!

25 - Refactoring: Composing Methods,

Moving Features and Organizing Data

CMPSCI 220 (291A)

Programming Methodology

Spring 2009

Goal for the week

 Build your catalog of familiar refactorings

 Increase sense of incremental nature of

refactoring

 Last time: Conditional Expressions (REF 9) and

Method Calls (REF 10)

 Today: Composing methods (REF 6), Moving

features around (REF 7), Data (REF 8)

 Next time: Generalization (REF 12) and Big

Refactorings (REF 13)

Composing Methods Refactorings

 All but one of these refactorings covered in

Lecture 18

 Extract Method

 Inline Method

 Replace Temp with Query

 Replace Method with Method Object

 Remaining refactoring for fairly special cases

 Split Temporary Variable

More Composing Methods

Refactorings

 Remove Assignments to Parameters

 Refactoring to overcome bad method design

 Substitute Algorithm

 After methods have been refactored, it’s clearer

how they work

 Better algorithm(s) may be apparent

Moving Features Between Objects

Refactorings -- Special Cases

 Source code for class not available

 But want to move responsibilities to it

 Introduce Foreign Method

 Adding one or two methods to unchangeable

class

 Introduce Local Extension

 Adding more than one or two methods to

unchangeable class

Refactoring: Move Method

 Method is using or is used by more features

of another class than of its defining class

 Create a new method with similar body in the

class it uses (or is used by) most

 Turn the old method into a simple delegation

 Or remove it altogether

Move Method: How?

 Declare method in the target class

 Copy code from source method

 Adjust references to source

 Determine how to access target object from

source

 Turn source method into delegating method

 Removal is optional

Example: Move Method

public class Account { double overdraftCharge() { if (_type.isPremium()) { double result = 10; if (_daysOverdrawn > 7) result += (_daysOverdrawn - 7) * 0.85; return result; } else return _daysOverdrawn * 1.75; } double bankCharge() { double result = 4.5; if (_daysOverdrawn > 0) result += overdraftCharge(); return result; } private AccountType _type; private int _daysOverdrawn; }

Example: Move Method (2)

class AccountType { boolean isPremium() {... } double overdraftCharge(int daysOverdrawn) { if (isPremium()) { double result = 10; if (daysOverdrawn > 7) result += (daysOverdrawn - 7) * 0.85; return result; } else return daysOverdrawn * 1.75; } } }

Example: Move Method (3)

public class Account { double overdraftCharge() { return _type.overdraftCharge(_daysOverdrawn); } double bankCharge() { double result = 4.5; if (_daysOverdrawn > 0) result += overdraftCharge(); return result; } private AccountType _type; private int _daysOverdrawn; }

• Source method body replaced with delegation

• Code now compiles and can be used as-is

Refactoring: Move Field

 Field is used by another class more than its

defining class

 Create a new field in the target class and change

all its users

 Sometimes the first step toward Extract Class

Move Field: How?

 If the field is public, use Encapsulate Field

 If clients are numerous or likely to move, may

want to use Self Encapsulate Field

 Compile and test

 Create a field in the target class with getter and

setter methods

 Compile target class

Example: Move Field

public class Account { private AccountType _type; private double _interestRate; double interestForAmount_days(double amount, int days) { return _interestRate * amount * days / 365; } }

• Want to move interest rate field to account type

• Among many references is interestForAmount_days

Example: Move Field (2)

class AccountType { private double _interestRate; double getInterestRate() { return _interestRate; } void setInterestRate(double arg) { _interestRate = arg; }

... }

• Create field and getter/setter methods

• Compile