A refactoring technique...
I'm not sure if I have done this before but I've just read about it and it's worth remembering.
Scenario: A class has a group of similar methods that vary by an enumeration or common value.
public IList LookupRetiredEmployees(int minAge) {
IList retVal = new ArrayList();
foreach (Employee emp in masterEmployeeCollection) {
if ((emp.Age >= minAge) && (emp.Status == EmpStatus.Retired))
retVal.Add(emp);
}
return retVal;
}
}
public IList LookupActiveEmployees(int minAge) {
IList retVal = new ArrayList();
foreach (Employee emp in masterEmployeeCollection) {
if ((emp.Age >= minAge) && (emp.Status == EmpStatus.Active))
retVal.Add(emp);
}
return retVal;
}
}
Reduce the methods to a single method that takes the enumeration or common value as a parameter.
public IList LookUpEmployees(int minAge, EmpStatus status) {
IList retVal = new ArrayList();
foreach (Employee emp in masterEmployeeCollection) {
if ((emp.Age >= minAge) && (emp.Status ==status))
retVal.Add(emp);
}
return retVal;
}
}