Property of Penn Engineering
Addressing Duplicate
Copyright By PowCoder代写 加微信 powcoder
Property of Penn Engineering
• Improving the internal quality of existing code
• Simplify future code changes
• Reduce the amount of code to maintain
• Always be on the lookout for code smells
Refactoring
Property of Penn Engineering
public static int maxArraySum(int[] array1, int[] array2) {
int sum1 = 0;
for (int i = 0; i < array1.length; i++) {
sum1 += array1[i];
int sum2 = 0;
for (int i = 0; i < array2.length; i++) {
sum2 += array2[i];
if (sum1 > sum2) {
return sum1;
return sum2;
What’s Wrong with This Code?
Property of Penn Engineering
public static int maxArraySum(int[] array1, int[] array2) {
int sum1 = 0;
for (int i = 0; i < array1.length; i++) {
if (array1[i] > 0) sum1 += array1[i];
int sum2 = 0;
for (int i = 0; i < array2.length; i++) {
sum2 += array2[i];
if (sum1 > sum2) {
return sum1;
return sum2;
What’s Wrong with This Code?
Property of Penn Engineering
public static int maxArraySum(int[] array1, int[] array2) {
int sum1 = 0;
for (int i = 0; i < array1.length; i++) {
if (array1[i] > 0) sum1 += array1[i];
int sum2 = 0;
for (int i = 0; i < array2.length; i++) {
if (array2[i] > 0) sum2 += array2[i];
if (sum1 > sum2) {
return sum1;
return sum2;
What’s Wrong with This Code?
Property of Penn Engineering
• #1 most common code smell
• Affects many aspects of internal quality
• Analyzability: more code to read
• Changeability: more places to change code
• Testability: more opportunities for bugs
• Solution: Extract Method
• Create a new method that can be used in multiple places
Duplicate Code
Property of Penn Engineering
public static int arrayMax
(int[] array1, int[] array2) {
int sum1 = 0;
for (int i = 0; i < array1.length; i++) {
sum1 += array1[i];
int sum2 = 0;
for (int i = 0; i < array2.length; i++) {
sum2 += array2[i];
if (sum1 > sum2) {
return sum1;
return sum2;
private static int
calculateArraySum(int[] arr) {
int arraySum = 0;
for (int i = 0;
i < arr.length; i++) { arraySum += arr[i]; return arraySum; Property of Penn Engineering public static int arrayMax (int[] array1, int[] array2) { int sum1 = calculateArraySum(array1); int sum2 = 0; for (int i = 0; i < array2.length; i++) { sum2 += array2[i]; if (sum1 > sum2) {
return sum1;
return sum2;
private static int
calculateArraySum(int[] arr) {
int arraySum = 0;
for (int i = 0;
i < arr.length; i++) {
arraySum += arr[i];
return arraySum;
Property of Penn Engineering
public static int arrayMax
(int[] array1, int[] array2) {
int sum1 = calculateArraySum(array1);
int sum2 = calculateArraySum(array2);
if (sum1 > sum2) {
return sum1;
return sum2;
private static int
calculateArraySum(int[] arr) {
int arraySum = 0;
for (int i = 0;
i < arr.length; i++) {
arraySum += arr[i];
return arraySum;
Property of Penn Engineering
public static int arrayMax
(int[] array1, int[] array2) {
int sum1 = calculateArraySum(array1);
int sum2 = calculateArraySum(array2);
if (sum1 > sum2) {
return sum1;
return sum2;
private static int
calculateArraySum(int[] arr) {
int arraySum = 0;
for (int i = 0;
i < arr.length; i++) { if(arr[i] > 0)
arraySum += arr[i];
return arraySum;
Property of Penn Engineering
• The Extract Method pattern assumes that the
duplicate code is in the same class
• When duplicate code is in separate classes, use the
Extract Superclass pattern
• Create a new common superclass that contains a
method that can be used in multiple subclasses
Duplicate Code in Separate Classes
Property of Penn Engineering
public class EventLogistics {
private int currentBudget;
protected double maxCapacity;
public EventLogistics() {
currentBudget = 0;
maxCapacity = 0;
public void updateWithProfits(int[] siteProfits) {
for (int i = 0; i < siteProfits.length; i++) {
currentBudget += siteProfits[i];
maxCapacity = Math.floor((float) currentBudget * 0.75);
EventLogistics Class
Property of Penn Engineering
public class EventLogistics {
private int currentBudget;
protected double maxCapacity;
public EventLogistics() {
currentBudget = 0;
maxCapacity = 0;
public void updateWithProfits(int[] siteProfits) {
for (int i = 0; i < siteProfits.length; i++) {
currentBudget += siteProfits[i];
maxCapacity = Math.floor((float) currentBudget * 0.75);
EventLogistics Class
Property of Penn Engineering
public class GrantLogistics {
private int currentBudget;
public GrantLogistics(int amountAvailable) {
currentBudget = amountAvailable;
public boolean fulfillAllRequests(int[] individualRequests) {
int totalAmountRequested = 0;
for (int i = 0; i < individualRequests.length; i++) {
totalAmountRequested += individualRequests[i];
return totalAmountRequested <= currentBudget;
GrantLogistics Class
Property of Penn Engineering
public class GrantLogistics {
private int currentBudget;
public GrantLogistics(int amountAvailable) {
currentBudget = amountAvailable;
public boolean fulfillAllRequests(int[] individualRequests) {
int totalAmountRequested = 0;
for (int i = 0; i < individualRequests.length; i++) {
totalAmountRequested += individualRequests[i];
return totalAmountRequested <= currentBudget;
GrantLogistics Class
Property of Penn Engineering
Extract Superclass
Property of Penn Engineering
Extract Superclass
Property of Penn Engineering
public abstract class FundCalculations {
protected int currentBudget;
public FundCalculations() {
currentBudget = 0;
protected int calculateSum(int[] arr) {
int arraySum = 0;
for (int i = 0; i < arr.length; i++) {
arraySum += arr[i];
return arraySum;
FundCalculations Class
Property of Penn Engineering
public abstract class FundCalculations {
protected int currentBudget;
public FundCalculations() {
currentBudget = 0;
protected int calculateSum(int[] arr) {
int arraySum = 0;
for (int i = 0; i < arr.length; i++) {
arraySum += arr[i];
return arraySum;
FundCalculations Class
Property of Penn Engineering
public class EventLogistics {
private int currentBudget;
protected double maxCapacity;
public EventLogistics() {
currentBudget = 0;
maxCapacity = 0;
public void updateWithProfits(int[] siteProfits) {
for (int i = 0; i < siteProfits.length; i++) {
currentBudget += siteProfits[i];
maxCapacity = Math.floor((float) currentBudget * 0.75);
Original EventLogistics Class
Property of Penn Engineering
public class EventLogistics {
private int currentBudget;
protected double maxCapacity;
public EventLogistics() {
currentBudget = 0;
maxCapacity = 0;
public void updateWithProfits(int[] siteProfits) {
for (int i = 0; i < siteProfits.length; i++) {
currentBudget += siteProfits[i];
maxCapacity = Math.floor((float) currentBudget * 0.75);
Original EventLogistics Class
Property of Penn Engineering
public class EventLogistics extends FundCalculations {
protected double maxCapacity;
public EventLogistics() {
maxCapacity = 0;
public void updateWithProfits(int[] siteProfits) {
currentBudget += calculateSum(siteProfits);
maxCapacity = Math.floor((float) currentBudget * 0.75);
Refactored EventLogistics Class
Property of Penn Engineering
public class GrantLogistics {
private int currentBudget;
public GrantLogistics(int amountAvailable) {
currentBudget = amountAvailable;
public boolean fulfillAllRequests(int[] individualRequests) {
int totalAmountRequested = 0;
for (int i = 0; i < individualRequests.length; i++) {
totalAmountRequested += individualRequests[i];
return totalAmountRequested <= currentBudget;
Original GrantLogistics Class
Property of Penn Engineering
public class GrantLogistics extends FundCalculations{
public GrantLogistics(int amountAvailable) {
currentBudget = amountAvailable;
public boolean fulfillAllRequests(int[] individualRequests) {
int totalAmountRequested = 0;
totalAmountRequested += calculateSum(individualRequests);
return totalAmountRequested <= currentBudget;
Refctored GrantLogistics Class
Property of Penn Engineering
Extract Superclass
Property of Penn Engineering
• Duplicate Code is the #1 most common code smell
• Extract Method: Create a new method that can be
used in multiple places within a single class
• Extract Superclass: Create a new common superclass
that contains a method that can be used in multiple
subclasses
Addressing Duplicate Code
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com