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
As far as I can tell, you won't have fewer methods, just fewer names. I generally prefer the overloaded method system of naming, but I don't think it really makes much difference as long as you comment and document your code well (which you should do in either case).
A brief glance at the framework should convince you that numerous overloads is an accepted state of affairs. In the face of myriad overloads, the design of overloads for usability is directly addressed by section 5.1.1 of the Microsoft Framework Design Guidelines (Kwalina and Abrams, 2006). Here is a brief prècis of that section:
DO try to use descriptive parameter names to indicate the default used by shorter overloads.
AVOID arbitrarily varying parameter names in overloads.
AVOID being inconsistent in the ordering of parameters in overloaded members.
DO make only the longest overload virtual (if extensibility is required). Shorter overloads should simply call through to a longer overload.
DO NOT use ref
or out
parameters to overload members.
DO allow null
to be passed for optional arguments.
DO use member overloading rather than defining members with default arguments.
Yes you can overuse it, however here is another concept which could help keep the usage of it under control ...
If you are using .Net 3.5+ and need to apply multiple filters you are probably better to use IQueryable and chaining i.e.
GetQuery<Type>().ApplyCategoryFilter(category).ApplyProductNameFilter(productName);
That way you can reuse the filtering logic over and over whereever you need it.
public static IQueryable<T> ApplyXYZFilter(this IQueryable<T> query, string filter)
{
return query.Where(XYZ => XYZ == filter);
}
yes you can overuse it. In your example it would seem like the first and third would probably return a single item, where the second would return several. If that is correct, then I would call the first and third GetProduct and the second GetProducts or GetProductList
if this is not the case and all three return several (as in if you pass it productID 5, it returns any items with 5 in the productid, or returns any items with the string parameter in its name) then I would call all three GetProducts or GetProductList and override all of them.
In any event, the name should reflect what the function does, so calling it GetProduct (singular) when it returns a list of Products doesn't make a good function name. IMNSHO