Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 because 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?

like image 504
TBone Avatar asked Jan 20 '23 11:01

TBone


1 Answers

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
like image 161
Jon Avatar answered Jan 28 '23 13:01

Jon