What\'s better practice when defining several methods that return the same shape of data with different filters? Explicit method names or overloaded methods?
For exa
The point of overloading to to ease the learning curve of someone using your code... and to allow you to use naming schemes that inform the user as to what the method does.
If you have ten different methods that all return a collection of employees, Then generating ten different names, (Especially if they start with different letters!) causes them to appear as multiple entries in your users' intellisense drop down, extending the length of the drop down, and hiding the distinction between the set of ten methods that all return an employee collection, and whatever other methods are in your class...
Think about what is already enforced by the .Net framework for, say constructors, and indexers... They are all forced to have the same name, and you can only create multiples by overloading them...
If you overload them, they will all appear as one, with their disparate signatures and comments off to tthe side.
You should not overload two methods if they perform different or unrelated functions...
As to the confusion that can exist when you want to overload two methods with the same signature by type as in
public List GetEmployees(int supervisorId);
public List GetEmployees(int departmentId); // Not Allowed !!
Well you can create separate types as wrappers for the offending core type to distinguish the signatures..
public struct EmployeeId
{
private int empId;
public int EmployeeId { get { return empId; } set { empId = value; } }
public EmployeeId(int employeId) { empId = employeeId; }
}
public struct DepartmentId
{
// analogous content
}
// Now it's fine, as the parameters are defined as distinct types...
public List GetEmployees(EmployeeId supervisorId);
public List GetEmployees(DepartmentId departmentId);