Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code refactoring homework?

Tags:

java

c++

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;

like image 763
Ali Nouman Avatar asked Nov 19 '11 09:11

Ali Nouman


People also ask

What is code refactoring example?

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.

How does code refactoring work?

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.


1 Answers

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;
like image 178
ComfortablyNumb Avatar answered Oct 13 '22 11:10

ComfortablyNumb