Java Refactoring — Clean Up Shipping Fee Code Without Changing the Rules
On this page
Refactoring improves the structure of code while preserving observable behavior, including return values, exceptions, data writes, and the order in which events occur. In this shipping example, we replace nested branches with separate calculations for the base fee and the remote-area surcharge, then add the two amounts.
Shipping fee rules
The inputs are a validated order subtotal, long subtotalWon in Korean won, membership status, boolean member, and whether the destination is remote, boolean remoteArea.
The base shipping fee is 3,000 won. Members with a subtotal of at least 50,000 won pay a base fee of zero. The 4,000-won remote-area surcharge applies regardless of the base fee waiver.
Split the nested branches into two fee calculations
shippingFeeBefore checks for a remote destination inside the waiver branch and repeats that check when the base fee applies. shippingFee names the threshold and fees as constants and calls isBaseFeeWaived to decide whether to waive the base fee. The method name makes the scope of the waiver explicit. The waiver condition and the surcharge calculation each appear in one place.
First determine the base fee, then determine the surcharge from the destination and return the sum.
Check the boundary
Each cell gives the expected shipping fee for its inputs. All amounts are in Korean won.
| Subtotal | Non-member · standard | Non-member · remote | Member · standard | Member · remote |
|---|---|---|---|---|
| 0 | 3,000 | 7,000 | 3,000 | 7,000 |
| 49,999 | 3,000 | 7,000 | 3,000 | 7,000 |
| 50,000 | 3,000 | 7,000 | 0 | 4,000 |
| 50,001 | 3,000 | 7,000 | 0 | 4,000 |
Save the code below as ShippingFeeDemo.java and run it with JDK 21. For each of the 16 inputs in the table, main compares the result of each method against the expected fee.
public class ShippingFeeDemo {
private static final long BASE_FEE_WAIVER_MIN_WON = 50_000L;
private static final int BASE_SHIPPING_FEE_WON = 3_000;
private static final int REMOTE_SURCHARGE_WON = 4_000;
public static int shippingFeeBefore(long subtotalWon, boolean member, boolean remoteArea) {
if (member && subtotalWon >= 50_000L) {
if (remoteArea) {
return 4_000;
}
return 0;
}
if (remoteArea) {
return 3_000 + 4_000;
}
return 3_000;
}
public static int shippingFee(long subtotalWon, boolean member, boolean remoteArea) {
int baseFee = isBaseFeeWaived(subtotalWon, member) ? 0 : BASE_SHIPPING_FEE_WON;
int surcharge = remoteArea ? REMOTE_SURCHARGE_WON : 0;
return baseFee + surcharge;
}
private static boolean isBaseFeeWaived(long subtotalWon, boolean member) {
return member && subtotalWon >= BASE_FEE_WAIVER_MIN_WON;
}
public static void main(String[] args) {
long[] subtotals = {0L, 49_999L, 50_000L, 50_001L};
int[][] expectedFees = {
{3_000, 7_000, 3_000, 7_000},
{3_000, 7_000, 3_000, 7_000},
{3_000, 7_000, 0, 4_000},
{3_000, 7_000, 0, 4_000}
};
for (int row = 0; row < subtotals.length; row++) {
long subtotal = subtotals[row];
for (int column = 0; column < expectedFees[row].length; column++) {
boolean member = column >= 2;
boolean remoteArea = column % 2 == 1;
int expected = expectedFees[row][column];
int before = shippingFeeBefore(subtotal, member, remoteArea);
int after = shippingFee(subtotal, member, remoteArea);
if (before != expected || after != expected) {
throw new AssertionError(
"subtotalWon=%d, member=%b, remoteArea=%b, expected=%d, before=%d, after=%d"
.formatted(subtotal, member, remoteArea, expected, before, after)
);
}
}
}
System.out.println("Both methods matched all 16 expected fees.");
}
}
javac --release 21 ShippingFeeDemo.java
java ShippingFeeDemo
Both methods matched all 16 expected fees on Zulu OpenJDK 21.0.1. The actual output was Both methods matched all 16 expected fees.. In a copy of the refactored method, changing >= to > made the check fail because a member with a subtotal of 50,000 won and a standard destination was charged 3,000 won instead of zero.
When applying the refactoring to existing code, keep the API signature that callers already use. Handle changes to the waiver threshold or fee policy separately.
Discussion