Download presentation
Presentation is loading. Please wait.
1
Refactorings Luke Marsh
2
Three Refactorings Replace Parameter With Method
Simplifying method calls Remove Assignment to Parameters Clarifying which objects are modified Hide Method Making methods private
3
Replace Parameters with Method
public double getPrice() { int basePrice = _quantity * _itemPrice; int discountLevel; if (_quantity > 100) discountLevel = 2; else discountLevel = 1; double finalPrice = discountedPrice (basePrice, discountLevel); return finalPrice; } private double discountedPrice (int basePrice, int discountLevel) { if (discountLevel == 2) return basePrice * 0.1; else return basePrice * 0.05; } Motivation: If it’s possible for the method to get a parameter by other means, it should do so. Long parameter lists should be simplified when possible.
4
Replace Parameters with Method
First Replacement: public double getPrice() { int basePrice = _quantity * _itemPrice; double finalPrice = discountedPrice (basePrice); return finalPrice; } private double discountedPrice (int basePrice) { if (getDiscountLevel() == 2) return basePrice * 0.1; else return basePrice * 0.05; } private int getDiscountLevel() { if (_quantity > 100) return 2; else return 1; }
5
Replace Parameters with Method
First Replacement: public double getPrice() { int basePrice = _quantity * _itemPrice; double finalPrice = discountedPrice (basePrice); return finalPrice; } private double discountedPrice (int basePrice) { if (getDiscountLevel() == 2) return basePrice * 0.1; else return basePrice * 0.05; } private int getDiscountLevel() { if (_quantity > 100) return 2; else return 1; } Second Replacement: public double getPrice() { return discountedPrice (); } private double discountedPrice () { if (getDiscountLevel() == 2) return getBasePrice() * 0.1; else return getBasePrice() * 0.05; } private double getBasePrice() { return _quantity * _itemPrice; }
6
Replace Parameters with Method
Benefit: Long parameters can cause confusion. The more parameters for a method, the more information the user needs to keep track of when calling the method.
7
Remove Assignments to Parameters
Example: int discount (int inputVal, int quantity, int yearToDate) { if (inputVal > 50) inputVal -= 2; if (quantity > 100) inputVal -= 1; if (yearToDate > 10000) inputVal -= 4; return inputVal; } Motivation: Changes to a parameter object, in a pass by value scenario, are not reflected in the calling routine. Furthermore, it leads to confusion in what object is being modified.
8
Remove Assignments to Parameters
Easy Solution: int discount (int inputVal, int quantity, int yearToDate) { int result = inputVal; if (inputVal > 50) result -= 2; if (quantity > 100) result -= 1; if (yearToDate > 10000) result -= 4; return result; } Benefit: No confusion as to which object is being modified, also makes this method function correctly in pass by value scenarios.
9
Hide Method Example: public int getMiles(){ public void setMiles (int x) { return miles; } miles = x; Motivation: Not all methods need to be visible, such as setter and getter methods. Solution: Set the method from public to private. Benefit: As your class grows, some methods don’t need to be accessed publicly, more specifically if it isn’t called by any other classes.
10
Any Questions?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.