There is a data structure idiom that looks something like this:

        key1: value1_1, 
        key2: value2_1
        key1: value1_2, 
        key2: value2_2

I've run into this a few times, and I have a number of issues with it. For instance, note that the objects do not actually have their IDs in the object, and that since they are in a list-of-dictionaries rather than a single dictionary, you can't even look them up by ID easily.

So my first question is whether there is a name for this idiom, and the follow-up would be pattern or anti-pattern? If pattern, why would you ever use it?

closed as primarily opinion-based by gnat, user40980, user22815, Kilian Foth, user53019 Jul 16 '15 at 18:21

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

  • I have seen this data structure before in open source projects that serve JSON. I'd consider this an emerging pattern. – Ben Brown Jul 14 '15 at 20:24
  • Yeah, and YAML makes it easy to express this in a way that looks perfectly human readable; but i think it's less efficient for the computer most of the time, and i'm wondering why someone would do it. – Vynce Jul 15 '15 at 1:50
  • For those voting this down, i'm curious why you think it's not worth asking, or how it should be worded better. – Vynce Jul 15 '15 at 1:51
  • Treat this as ordered multimap. – Basilevs Jul 15 '15 at 3:29

It's neither a pattern nor an anti-pattern. There's no specific name for it. It is exactly what it appears to be and its quality is determined by the program using it.

Does it make sense? Not to me, not without context. Could the outer array be removed and the IDs moved into a single object? Maybe, depends on the program. Could the nested objects be flattened to include the ID and properties at the same depth? Maybe, depends on the program. Could you do both and have only a single object? See above.

If you're having trouble working with this data, then either hide it behind an interface or copy the data into a structure more suitable for your usage.

  • 1
    "Maybe, depends" -- on what? I'm asking what sort of situation would make this actually good. As for whether i'm having trouble with it -- this isn't about a particular case. It's about the fact that I am seeing this somewhat often and wondering why. – Vynce Jul 14 '15 at 20:36
  • @Vynce There are countless programs that could consume this data structure. Is the ordering of the records in the array important? Impossible to tell. It could have been a list, a set, a bag, or something else in the original data, but JSON only has lists and maps. One program could have used a list, another could have used a set, and the JSON would look the same. Can the same ID show up in multiple records? Impossible to tell without documentation. If you've seen this structure before, then try examining the programs that use it for more concrete examples. – Eric Jul 15 '15 at 12:34
  • "Can consume" is not "Is better with". And I have examined. In this case, for instance, the order does not appear important, and the IDs should not repeat. And wouldn't it make more sense to include the ID as one of the values in the object anyway? I understand that you think there could be a situation, but having seen it repeatedly, without any clear reason, I suspect that either there's something i'm missing -- a justification that makes it a pattern -- or it's actually an anti-pattern that people create without thinking. Hence the question. – Vynce Jul 15 '15 at 16:41
  • @Vynce It's neither a pattern nor an anti-pattern. Period. These terms do not apply to simple data structures. I have answered your question. Programmers sometimes use sub-optimal data structures reasons that aren't readily apparent, such as compatibility or familiarity. There's nothing inherently special about List<Map<String, Map<String, Object>>>. – Eric Jul 15 '15 at 18:44
  • the tone of your answer, and comments, implied that you thought it was a stupid question, and that the things you were saying are related by a causal relationship that is not clear. Are you now saying that a data structure is never a pattern or an anti-pattern? I would doubt that assertion without citation. In any case, you have no answered "why would you ever use it?" You have only said, essentially, "when it's right." And you have asserted that it has no name, which may be true, but perhaps better to say only that you know of none, and expect none, unless you have an exhaustive list. cite? – Vynce Jul 16 '15 at 20:18

The data structure that you're describing is often used in JSON documents where you have a list of objects, each with attributes (keys to dictionaries) and their values (the mapped portion of each dictionary). In the DS that you give, you can think of obj_1_id and obj_2_id as objects and key1 and key2 as those objects' fields.

Not the answer you're looking for? Browse other questions tagged or ask your own question.