Returning dictionaries from methods
These types of methods have always bothered me. Without digging through the code, I have no idea what the key or sub-value is supposed to be in this dictionary. I've done this myself about a thousand times, I'm sure. But it has always bothered me bec开发者_开发知识库ause it lacks so much information.
IDictionary<int,HashSet<int>> GetExportedCharges()
{
...
}
void GetVisitStatuses(IDictionary<int,HashSet<int>> exportedCharges)
{
...
}
Having the material in a dictionary makes sense, but all of the IDictionary methods have very abstract parameters. I suppose I could create a new class, implement IDictionary, and rename all of the parameters. It just seems like overkill. Makes be wish c# had a 'typdef' directive.
How do you avoid returning dictionaries like this? Or do you avoid this at all?
There is a very simple solution that you might find adequate:
public class IdToSetOfWidgetNumbersMap : Dictionary<int,HashSet<int>> { }
Advantages:
- Seeing a method returning an
IdToSetOfWidgetNumbersMap
doesn't leave many questions open - The class can be left totally empty, or you can choose to provide aliases for the members exposed by
Dictionary
if you prefer
Disadvantage:
- You need to create a do-nothing class for each type of dictionary
Personally I don't avoid returning dictionaries.
I agree, having only method signature it all looks very vague. So my rule is to ALWAYS write comments in a style "Dictionary (logical description of the keys) - (logical description of values)". And to hope that I will not need to use something horrible like
Dictionary<SomeType, <Dictionary<...>>
精彩评论