Assignment 8: Refactoring Solution

$30.00

Description

For the assignment, you will need to find out information about refactoring patterns, which does not appear in the lecture slides. The following location is an excellent starting point:http://www.refactoring.com/catalog/index.html.

1. Consider the following two code fragments.

Which fragment is more understandable? And why? (Hint: Think code smells)

A.

lu1973dkjy4u tmp 5eb897ded4037f01

doublepotentialEnergy(double mass, double height) {

return mass * height * 9.81;

}

B.

lu1973dkjy4u tmp 9ae306360fa0889d lu1973dkjy4u tmp 491ed708a6b3d7a2

static final double g = 9.81;

doublepotentialEnergy(double mass, double height) {

return mass * g * height;

1 of 7 , 9:21 p.m.

Assignment https://eclass.srv.ualberta.ca/mod/assign/view.php?id=3237750

lu1973dkjy4u tmp efe22e235b26e7e4

}

2. Consider the following code fragment.

lu1973dkjy4u tmp 29ea584e8b58bfdb

publicintfunny(int a, int b) {

int temp = a * b;

if (temp > 100) {

return temp * 0.95;

} else {

return temp * 0.25;

}

}

Apply the following refactoring patterns to this code fragment:

A. Inline Temp

B. Extract Method Followed by Replace Temp with Query

C. Discuss the two new code fragments, which refactoring pattern is more appropriate in this situation? And why?

3. Consider the following two code fragments.

A.

lu1973dkjy4u tmp 5d76eeeff23ff850

privateint currentBalance;

intwithdrawFromBankAccount(int amountToBeWithdrawn) {

if (amountToBeWithdrawn > currentBalance)

return –1;

else {

currentBalance -= amountToBeWithdrawn;

return 0;

}

}

2 of 7 , 9:21 p.m.

B.

lu1973dkjy4u tmp 725a85fdb256d57c

privateint currentBalance;

voidwithdrawFromBankAccount(int amountToBeWithdrawn) throws BalanceException { if (amountToBeWithdrawn > currentBalance)

throw new BalanceException();// You can assume that BalanceException is defined

currentBalance -= amountToBeWithdrawn;

}

Which refactoring pattern has been applied to the first fragment to transform it into the second code fragment? Explain why the second code fragment is superior to the first fragment.

4. Consider the following two code fragments (adaptedfrom lu1973dkjy4u tmp 3f98aef390250807 java.lang.Longlu1973dkjy4u tmp 8ffe520699ccb1c2 ).

lu1973dkjy4u tmp 649a1fa4a50f2c5e

A.

lu1973dkjy4u tmp b71ec4ddcfc58355

public static Long valueOf(long l) {

final int offset = 128;

if (l >= – 128 && l <= 127) {// will cache

return LongCache.cache[(int) l + offset];

}

return new Long(l);

}

B.

3 of 7 , 9:21 p.m.

Assignment https://eclass.srv.ualberta.ca/mod/assign/view.php?id=3237750

lu1973dkjy4u tmp 299fc93dd72acb89

public static Long valueOf(long l) {

if (l >= – 128 && l <= 127) {// will cache

return LongCache.cache[(int) l + 128];

}

return new Long(l);

}

Fragment A is translated into Fragment B by applying the Inline Temp refactoring; and Fragment B is translated into Fragment A by applying the Introduce Explaining Variable refactoring. Provide possible situations (or contexts) where it would be advantageous to apply either refactoring to the appropriate code fragment.

5. Consider the following two code fragments.

A.

lu1973dkjy4u tmp 5a257a90e6aa3894

public class A {

public intk(long i) {

return 10;

}

}

public class B extends A {

public intk(int i) {

return 20;

}

public static voidmain(String[] args) {

System.out.println(new A().k(2));

}

}

B.

public class A {

public intk(long i) {

return 10;

}

public intk(int i) {

return 20;

}

}

public class B extends A {

public static voidmain(String[] args) {

System.out.println(new A().k(2));

}

}

Which refactoring pattern has been applied to the first fragment to transform it into the second code fragment? Explain the impact (upon the results from function test) of the transformation.

lu1973dkjy4u tmp c2f643d37b98ce97