Intermediate 25 min read

Review and Refactor

By the end of this lesson, you'll be able to:

  • Identify duplicated stock-fetching logic across InventoryService methods
  • Extract a shared private helper method to remove that duplication
  • Confirm the refactor preserves every method's existing behavior

Prerequisites: "Handle Errors"

Spotting the duplication

public void receiveShipment(Id warehouseStockId, Decimal quantityReceived) {
    Warehouse_Stock__c stock = [SELECT Id, Quantity_On_Hand__c FROM Warehouse_Stock__c WHERE Id = :warehouseStockId];
    // ...
}

public void shipOrder(Id warehouseStockId, Decimal quantityShipped) {
    Warehouse_Stock__c stock = [SELECT Id, Quantity_On_Hand__c FROM Warehouse_Stock__c WHERE Id = :warehouseStockId];
    // ...
}

Both methods query Warehouse_Stock__c by Id with the exact same field list — the same "fetch this one stock record" shape, written out twice.

Extracting a shared private helper

private Warehouse_Stock__c getStockById(Id warehouseStockId) {
    return [SELECT Id, Quantity_On_Hand__c FROM Warehouse_Stock__c WHERE Id = :warehouseStockId];
}

public void receiveShipment(Id warehouseStockId, Decimal quantityReceived) {
    Warehouse_Stock__c stock = getStockById(warehouseStockId);
    stock.Quantity_On_Hand__c += quantityReceived;
    update stock;
}

public void shipOrder(Id warehouseStockId, Decimal quantityShipped) {
    Warehouse_Stock__c stock = getStockById(warehouseStockId);
    if (stock.Quantity_On_Hand__c < quantityShipped) {
        throw new InventoryException('Not enough stock to ship this quantity.');
    }
    stock.Quantity_On_Hand__c -= quantityShipped;
    update stock;
}

getStockById is private, exactly like the extracted helpers in every previous project module — a change to which fields are fetched now happens in one place, not two (or more, as the class grows further).

The pattern across all three project modules

This closes out the third straight project module ending in the same "extract the repeated fetch-by-Id logic into a private helper" refactor — Student lookups in Module 12, Employee lookups in Module 17, Book__c lookups in Module 22, and now Warehouse_Stock__c lookups here. Recognizing this as a repeating pattern, not a coincidence, is itself a useful thing to notice: "fetch one record by Id, used by several methods" is common enough to reach for a shared private helper almost by default in real Apex classes.

Exercise

Refactor getLowStockItems to call a new getAllStock() private helper (returning every Warehouse_Stock__c) and filter it, instead of writing its own WHERE clause. Keep the same result.

Show hint

This time, the refactor changes the filtering approach — note the trade-off from pulling all records into Apex versus filtering in SOQL directly.

APEX

Review and Refactor Quiz

1. What repeating pattern did this module's final lesson notice across Modules 12, 17, 22, and 23?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

The final lesson reviews InventoryService as a whole, extracting the repeated "fetch this stock record by Id" pattern — the same refactoring discipline that closed out Modules 12, 17, and 22.