This is the code that I have to refactor for my homework:
if (state == TEXAS) {
rate = TX_RATE;
amt = base * TX_RATE;
calc = 2 * basis(amt) + extra(amt) * 1.05;
} else if ((state == OHIO) || (state == MAINE)) {
rate = (state == OHIO) ? OH_RATE : MN_RATE;
amt = base * rate;
calc = 2 * basis(amt) + extra(amt) * 1.05;
if (state == OHIO)
points = 2;
} else {
rate = 1;
amt = base;
calc = 2 * basis(amt) + extra(amt) * 1.05;
}
I have done something like this
if (state == TEXAS) {
rate = TX_RATE;
calculation(rate);
}
else if ((state == OHIO) || (state == MAINE))
{
rate = (state == OHIO) ? OH_RATE : MN_RATE;
calculation(rate);
if (state == OHIO)
points = 2;
}
else {
rate = 1;
calculation(rate);
}
function calculation(rate)
{
amt = base * rate;
calc = 2 * basis(amt) + extra(amt) * 1.05;
}
How could I have done better?
Edit i have done code edit
amt = base * rate;
Examples are: adding, removing, and introducing new parameters, replacing the parameter with the explicit method and method call, parameterize method, making a separate query from modifier, preserve the whole object, remove setting method, etc.
Refactoring is the process of restructuring code, while not changing its original functionality. The goal of refactoring is to improve internal code by making many small changes without altering the code's external behavior.
class State {
private :
double taxRate;
int baseWeight;
int extraWeight;
string name;
base;
public:
State(string name, double taxRate = 1, int point =0, double baseWeight=2, double extraWeight=1.05); //implement the method yourself
double extra(double base);
double basis(double base);
double calculate(double base){
return baseWeight * basis(base) + baseWeight * extra(base);
}
int point(){return point};
};
Now how to use it:
State ohio ("OHIO", OH_RATE, 2);
cout << "OHIO result:" ohio.calculate() << " point:" << ohio.point() << endl;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With