Code refactoring homework?

Posted by Hira on Stack Overflow See other posts from Stack Overflow or by Hira
Published on 2011-11-19T09:08:13Z Indexed on 2011/11/19 9:50 UTC
Read the original article Hit count: 378

Filed under:
|
|

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;

© Stack Overflow or respective owner

Related posts about java

Related posts about c++