Intermediate 25 min read

Review and Refactor

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

  • Identify duplicated validation logic across methods
  • Extract a private helper method to remove that duplication
  • Confirm the refactor doesn't change external behavior

Prerequisites: "Handle Edge Cases"

Spotting the duplication

public Boolean addEmployee(Employee emp) {
    if (emp == null || String.isBlank(emp.getEmployeeId())) {
        return false;
    }
    if (employeesById.containsKey(emp.getEmployeeId())) {
        return false;
    }
    employeesById.put(emp.getEmployeeId(), emp);
    return true;
}

public Boolean updateEmployee(Employee emp) {
    if (emp == null || String.isBlank(emp.getEmployeeId())) {
        return false;
    }
    if (! employeesById.containsKey(emp.getEmployeeId())) {
        return false;
    }
    employeesById.put(emp.getEmployeeId(), emp);
    return true;
}
  1. public Boolean addEmployee(Employee emp) { — the add method.
  2. if (emp == null || String.isBlank(emp.getEmployeeId())) { — the "is this a valid, non-null employee with an ID?" check.
  3. return false; — rejects invalid input.
  4. } — closes the if.
  5. if (employeesById.containsKey(emp.getEmployeeId())) { — the add-specific rule: reject a duplicate.
  6. return false; — rejects it.
  7. } — closes the if.
  8. employeesById.put(emp.getEmployeeId(), emp); — adds the employee.
  9. return true; — reports success.
  10. } — closes addEmployee.
  11. (blank line)
  12. public Boolean updateEmployee(Employee emp) { — the update method.
  13. if (emp == null || String.isBlank(emp.getEmployeeId())) { — the exact same validity check, duplicated.
  14. return false; — rejects invalid input.
  15. } — closes the if.
  16. if (! employeesById.containsKey(emp.getEmployeeId())) { — the update-specific rule: reject if it doesn't already exist.
  17. return false; — rejects it.
  18. } — closes the if.
  19. employeesById.put(emp.getEmployeeId(), emp); — updates the employee.
  20. return true; — reports success.
  21. } — closes updateEmployee.

Both methods start with the exact same check — duplicated logic that would need updating in two places if the validation rule ever changed.

Extracting a shared helper

public Boolean addEmployee(Employee emp) {
    if (! isValidEmployee(emp) || employeesById.containsKey(emp.getEmployeeId())) {
        return false;
    }
    employeesById.put(emp.getEmployeeId(), emp);
    return true;
}

public Boolean updateEmployee(Employee emp) {
    if (! isValidEmployee(emp) || ! employeesById.containsKey(emp.getEmployeeId())) {
        return false;
    }
    employeesById.put(emp.getEmployeeId(), emp);
    return true;
}

private Boolean isValidEmployee(Employee emp) {
    return emp != null && String.isNotBlank(emp.getEmployeeId());
}
  1. public Boolean addEmployee(Employee emp) { — now shorter.
  2. if (! isValidEmployee(emp) || employeesById.containsKey(emp.getEmployeeId())) { — the validity check is now a single delegated call.
  3. return false; — rejects.
  4. } — closes the if.
  5. employeesById.put(emp.getEmployeeId(), emp); — adds the employee.
  6. return true; — reports success.
  7. } — closes addEmployee.
  8. (blank line)
  9. public Boolean updateEmployee(Employee emp) { — also shorter.
  10. if (! isValidEmployee(emp) || ! employeesById.containsKey(emp.getEmployeeId())) { — reuses the same helper.
  11. return false; — rejects.
  12. } — closes the if.
  13. employeesById.put(emp.getEmployeeId(), emp); — updates the employee.
  14. return true; — reports success.
  15. } — closes updateEmployee.
  16. (blank line)
  17. private Boolean isValidEmployee(Employee emp) { — private — an internal implementation detail, exactly like Module 12's isAlreadyEnrolled and isFull helpers.
  18. return emp != null && String.isNotBlank(emp.getEmployeeId()); — the one, now-shared validation rule.
  19. } — closes the method.

The validation rule now lives in exactly one place; changing it later means editing one method instead of hunting down every duplicate copy.

Confirming behavior hasn't changed

EmployeeDirectory directory = new EmployeeDirectory();

directory.addEmployee(new Employee('E001', 'Amara Nkosi', 'Engineering')); // true, same as before
directory.addEmployee(null); // false, same as before
directory.updateEmployee(new Employee('E999', 'Nobody', 'Sales')); // false, same as before — E999 was never added
  1. EmployeeDirectory directory = new EmployeeDirectory(); — a fresh directory.
  2. (blank line)
  3. directory.addEmployee(new Employee('E001', 'Amara Nkosi', 'Engineering')); — true, same as before the refactor.
  4. directory.addEmployee(null); — false, same as before.
  5. directory.updateEmployee(new Employee('E999', 'Nobody', 'Sales')); — false, same as before — E999 was never added.

Every existing call still returns exactly what it did before the refactor — this is the whole point of refactoring: the internals got clearer, but nothing about how EmployeeDirectory behaves from the outside changed at all.

Exercise

Extract a private helper isValidDepartment(String department) that returns true when the department is not blank, and use it inside a method setDepartment(Employee emp, String department) that only proceeds if valid.

Show hint

private Boolean isValidDepartment(String department) { return String.isNotBlank(department); }

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

Review and Refactor Quiz

1. What is the goal of extracting duplicated validation logic into a shared private method?

2. A good refactor changes a class's external behavior for the better.

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 EmployeeDirectory as a whole and extracts a small, repeated validation check into its own private method — the same refactoring discipline from Module 12's final lesson.