1. Chapter 10 Making Method Calls Simpler

1.1. Rename Method

The name of a method does not reveal its purpose.
Change the name of the method



1.2. Add Parameter

A method needs more information from its caller.
Add a parameter for an object that can pass on this information


1.3. Remove Parameter

A parameter is no longer used by the method body.
Remove it


1.4. 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


1.5. 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


1.6. 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

~cpp 
void setValue (String name, int value) {
	if (name.equals("height"))
		_height = value;
	if (name.equals("width"))
		_width = value;
	Assert.shouldNeverReachHere();
}

~cpp 
void setHeight (int arg) {
	_height = arg;
}
void setWidth (int arg) {
	_width = arg;
}

1.7. 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

~cpp 
int low = daysTempRange().getLow();
int high = days.TempRange().getHight();
withinPlan = plan.withinRange (low, high);

~cpp 
withinPlan = plan.withinRange (daysTempRange());

1.8. 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

~cpp 
int basePrice = _quantity * _itemPrice;
discountLevel = getDiscountLevel ();
double finalPrice = discountedPrice (basePrice, discountLevel);

~cpp 
int basePrice = _quantity * _itemPrice;
double finalPrice = discountedPrice (basePrice);

1.9. Introduce Parameter Object

You have a group of parameters that naturally go together.
Replace them with an object


1.10. Remove Setting Method

A field should be set at creation time and never altered.
Remove any setting method for that field


1.11. Hide Method

A method is not used by any other class.
Make the method private


1.12. 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


~cpp 
Emplyee (int type) {
	_type = type;
}

~cpp 
static Emplyee create (int type) {
	return new Emplyee (type);
}

1.13. Encapsulate Downcast

A method returns an object that needs to be downcasted by its callers.
Move the downcast to within the method

~cpp 
Object lastReading () {
	return readings.lastElement ();
}

~cpp 
Reading lastReading () {
	return (Reading) readings.lastElement ();
}

1.14. Replace Error Code with Exception

A method returns a special code to indicate an error.
Throw an exception instead

~cpp 
int withdraw(int amount) {
	if (amount > _balance)
		return -1;
	else {
		_balance -= amount;
		return 0;
	}
}


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

1.15. 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

~cpp 
double getValueForPeriod (int periodNumber) {
	try {
		return _values[periodNumber];
	} catch (ArrayIndexOutOfBoundsException e) {
		return 0;
	}
}

~cpp 
double getValueForPeriod (int periodNumber) {
	if (periodNumber >= _values.length) return 0;
	return _values[periodNumber];
}
Retrieved from http://wiki.zeropage.org/wiki.php/Refactoring/MakingMethodCallsSimpler
last modified 2021-02-07 05:27:53