Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a deep nested Dictionary an antipattern?

I have a structure that can be very easily represented using a three-deep nested dictionary, like so

private static Dictionary<string, Dictionary<string, Dictionary<string,string>>> PrerenderedTemplates;

Where the structure might be used something like this

PrerenderedTemplates[instanceID][templategroup][templatepart]

Now, I realise that this code is hard to read, because from looking at the definition statement, you can't tell what it's being used for. The only advantage I can really see in changing it to Dictionary<string, PrerenderedTemplate> is readability. Converting each nesting into its own class (e.g class PrerenderedTemplate{} class TemplateGroup{} class TemplatePart{}) would add many more lines of code for little (if any) computational advantage. As far as I can see.

  • So, is my approach "ok" or should I go the extra mile and create seperate classes?
  • Is it okay to cover how the nested Dictionary works in the documentation/comments
  • Is there a best practice for handling this sort of nesting?
  • Bear in mind, this is a private member, it doesn't need to be straightforward for people using the class.

Update

So, inspired by Reza, but unable to use Tuples, I decided to create my own key generator and implement his pattern like this:

private Dictionary<string, string> PrerenderedTemplates;
private string GetPrerenderedTemplateKey(string InstanceId, string FeatureId, string OptionId)
{
    return new StringBuilder(instanceId)
    .Append(FormatTools.LIST_ENTRY_DELIMITER)
    .Append(templategroup)
    .Append(FormatTools.LIST_ENTRY_DELIMITER)
    .Append(templatepart).ToString();
}

Where FormatTools.LIST_ENTRY_DELIMITER is the Unicode Private Use Character 0xe04d.

like image 947
Iain Fraser Avatar asked Feb 16 '12 00:02

Iain Fraser


People also ask

What is a flattened dictionary Python?

flatdict is a Python library that creates a single level dict from a nested one and is available from Python 3.5 onwards. We've seen so far that writing our custom solution may not be ideal, and using a full-blown library like pandas just for this purpose is not great either.

Can Python dictionaries be nested to any depth?

Dictionaries can be nested to any depth. Dictionaries are mutable. All the keys in a dictionary must be of the same type. Dictionaries are accessed by key.

Which is faster tuple or dictionary in C#?

The Tuple method is similar to the above snippets, and while it is faster than the Dictionary<int, KeyValuePair<string, string>> , it is still nowhere near as fast as indexing directly into the collection to the desired value based on a hashed key, as is done in the MultiKeyDictionary class.

How do you pop a nested dictionary in Python?

To remove an element from a nested dictionary, use the del() method.


1 Answers

I offer another choice:

Dictionary<Tuple<string, string, string>, string> pt;

Access to dictionary:

pt[Tuple.Create("id","group","part")]

UPDATE:

Value Tuples introduced in C# 7 is most eye-catching:

Dictionary<(string id, string group, string part), string> pt;

Access to dictionary:

pt[("id", "group", "part")]
like image 100
Reza ArabQaeni Avatar answered Sep 19 '22 15:09

Reza ArabQaeni