Refactorings Methods

A tool for learn to identify bad Smell code and refactoring methods

98 cards   |   Total Attempts: 188
  

Cards In This Set

Front Back
Void printOwing() { Enumeration e = _orders.elements(); double outstanding = 0.0; // print banner System.out.println ("**************************"); System.out.println ("***** Customer Owes ******"); System.out.println ("**************************"); // calculate outstanding while (e.hasMoreElements()) { Order each = (Order) e.nextElement(); outstanding += each.getAmount(); } //print details System.out.println ("name:" + _name); System.out.println ("amount" + outstanding); }
Extract Method

secundary

Remove Assignments to Parameters.
Int getRating() { return (moreThanFiveLateDeliveries()) ? 2 : 1; } boolean moreThanFiveLateDeliveries() { return _numberOfLateDeliveries > 5; }
Composing Methods Inline Method
double basePrice = anOrder.basePrice(); return (basePrice > 1000)
Composing Methods Inline Temp
return (anOrder.basePrice() > 1000)
Double getPrice() { int basePrice = _quantity * _itemPrice; double discountFactor; if (basePrice > 1000) discountFactor = 0.95; else discountFactor = 0.98; return basePrice * discountFactor; }
Composing Methods Replace Temp with Query
Double price() { // price is base price - quantity discount + shipping return _quantity * _itemPrice - Math.max(0, _quantity - 500) * _itemPrice * 0.05 + Math.min(_quantity * _itemPrice * 0.1, 100.0); }
Composing Methods

Introduce Explaining Variable
Double getDistanceTravelled (int time) { double result; double acc = _primaryForce / _mass; int primaryTime = Math.min(time, _delay); result = 0.5 * acc * primaryTime * primaryTime; int secondaryTime = time - _delay; if (secondaryTime > 0) { double primaryVel = acc * _delay; acc = (_primaryForce + _secondaryForce) / _mass; result += primaryVel * secondaryTime + 0.5 * acc * secondaryTime * secondaryTime; } return result; }
Composing Methods Split Temporary Variable
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; }
Composing Methods Remove Assignments to Parameters
//Class Account int gamma (int inputVal, int quantity, int yearToDate) { int importantValue1 = (inputVal * quantity) + delta(); int importantValue2 = (inputVal * yearToDate) + 100; if ((yearToDate - importantValue1) > 100) importantValue2 -= 20; int importantValue3 = importantValue2 * 7; // and so on... return importantValue3 - 2 * importantValue1; }
Composing Methods

Replace Method with Method Object
String foundPerson(String[] people){ for (int i = 0; i < people.length; i++) { if (people[i].equals ("Don")){ return "Don"; } if (people[i].equals ("John")){ return "John"; } if (people[i].equals ("Kent")){ eturn "Kent"; } } return ""; }
Composing Methods Substitute Algorithm
Refactorings sections
Composing Methods
Moving Features Between Objects
Organizing Data
Simplifying Conditional Expressions
Making Method Calls Simpler
Dealing with Generalization
Big Refactorings
Question 11
Name of the refactoring in the image
Moving Features Between Objects

Move Method
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;
Moving Features Between Objects Move Method
Question 13
Refactor on image is?
Moving Features Between Objects Move Field
class Account... private AccountType _type; private double _interestRate; double interestForAmount_days (double amount, int days) { return _interestRate * amount * days / 365; }
Moving Features Between Objects Move Field
class Account... private AccountType _type; private double _interestRate; double interestForAmount_days (double amount, int days) { return getInterestRate() * amount * days / 365; } private void setInterestRate (double arg) { _interestRate = arg; } private double getInterestRate () { return _interestRate; }
Moving Features Between Objects Move FieldIf a lot of methods use the interest rate field, I might start by using Self Encapsulate Field: