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;
}
Both methods start with the exact same "is this a valid, non-null employee with an ID?" 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());
}
isValidEmployee is private — an internal implementation detail, exactly like Module 12's isAlreadyEnrolled and isFull helpers. 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
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); }
Review and Refactor Quiz
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.