Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Refactoring/MakingMethodCallsSimpler

From ZeroWiki
Revision as of 00:29, 27 March 2026 by Maintenance script (talk | contribs) (Repair batch-0003 pages from live compare)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Chapter 10 Making Method Calls Simpler

Rename Method

The name of a method does not reveal its purpose.

Change the name of the method

http://zeropage.org/~reset/zb/data/RenameMethod.gif

Add Parameter

A method needs more information from its caller.

Add a parameter for an object that can pass on this information

http://zeropage.org/~reset/zb/data/AddParameter.gif

Remove Parameter

A parameter is no longer used by the method body.

Remove it

http://zeropage.org/~reset/zb/data/RemoveParameter.gif

Separate Query from Modifier

You have a method that returns a value but also changes the state of an object.

Create two methods, one for the query and one for the modification

http://zeropage.org/~reset/zb/data/SeparateQueryFromModifier.gif

Parameterize Method

Several methods do similar things but with different values contained in the method body.

Create one method that uses a parameter for the different values

http://zeropage.org/~reset/zb/data/ParameterizeMethod.gif

Replace Paramter with Explicit Method

You have a method that runs different code depending on the values of an enumerated parameter.

Create a separate method for each value of the parameter
void setValue (String name, int value) {
	if (name.equals("height"))
		_height = value;
	if (name.equals("width"))
		_width = value;
	Assert.shouldNeverReachHere();
}
void setHeight (int arg) {
	_height = arg;
}
void setWidth (int arg) {
	_width = arg;
}

Preserve Whole Object

You are getting several values from an object and passing these values as parameters in a method call.

Send the whole object instead
int low = daysTempRange().getLow();
int high = days.TempRange().getHight();
withinPlan = plan.withinRange (low, high);
withinPlan = plan.withinRange (daysTempRange());

Replace Parameter with Method

An object invokes a method, then passes the result as a parameter for a method. The receiver can also invoke this method.

Remove the parameter and let the receiver invoke the method
int basePrice = _quantity * _itemPrice;
discountLevel = getDiscountLevel ();
double finalPrice = discountedPrice (basePrice, discountLevel);
int basePrice = _quantity * _itemPrice;
double finalPrice = discountedPrice (basePrice);

Introduce Parameter Object

You have a group of parameters that naturally go together.

Replace them with an object

http://zeropage.org/~reset/zb/data/IntroduceParameterObject.gif

Remove Setting Method

A field should be set at creation time and never altered.

Remove any setting method for that field

http://zeropage.org/~reset/zb/data/RemoveSettingMethod.gif

Hide Method

A method is not used by any other class.

Make the method private

http://zeropage.org/~reset/zb/data/HideMethod.gif

Replace Constructor with Factory Method

You want to do more that simple construction when you create an object.

Replace the constructor with a factory method
Emplyee (int type) {
	_type = type;
}
static Emplyee create (int type) {
	return new Emplyee (type);
}

Encapsulate Downcast

A method returns an object that needs to be downcasted by its callers.

Move the downcast to within the method
Object lastReading () {
	return readings.lastElement ();
}
Reading lastReading () {
	return (Reading) readings.lastElement ();
}

Replace Error Code with Exception

A method returns a special code to indicate an error.

Throw an exception instead
int withdraw(int amount) {
	if (amount > _balance)
		return -1;
	else {
		_balance -= amount;
		return 0;
	}
}


void withdraw(int amount) throws BalanceException {
	if (amount > _balance) throw new BalanceException ();
	_balance -= amount;
}

Replace Exception with Test

You are throwing an exception on a condition the caller could have checked first.

Change the caller to make the test first
double getValueForPeriod (int periodNumber) {
	try {
		return _values[periodNumber];
	} catch (ArrayIndexOutOfBoundsException e) {
		return 0;
	}
}
double getValueForPeriod (int periodNumber) {
	if (periodNumber >= _values.length) return 0;
	return _values[periodNumber];
}

Refactoring