2

A dictionary that returns the key as the value by default can be used in cases where exceptions or translation is needed.

E.g. One could do

i18n = SpecialDict()

if spanish:
    i18n["Hello"] = "Hola"

print(i18n["Test"]) # prints Test
print(i18n["Hello"]) # prints Hola if spanish

I18n is just an example, there are other (probably better) uses.

Is there a name for this dictionary / pattern?

Edit

Wow, I did not expect such a backsplash. Just to make it clear, this type of dictionary is not "defective" or "broken". It is a simple and elegant solution for a specific type of problem, and I have used this pattern with great success in my career. If you fail to see a usage for this then please move along to another question.

As for gnat's comment, I was not aware that questions like this are not welcome here. I'm sorry for that. I would delete my question but there is already an answer (which actually does not answer my question at all) so I'll just flag it for closure.

Edit 2

Since I have not found a name or usage of this, and this (sadly short-lived) question did not bring up anything either I hereby name this pattern

Data Aspect

and the dictionary-like object a

Data Aspect Map

I would love to post an answer to this question with a more detailed definition, description and maybe some examples and welcome feedback to this pattern, but I can't, since this question is on hold.

closed as primarily opinion-based by gnat, user40980, Robert Harvey, user22815, Arseni Mourzenko Feb 25 '15 at 19:02

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.

  • 2
    I'd call it a bad dictionary. I'd be ok with a function that tries to retrieve something from a dictionary and returns the key otherwise. I wouldn't be OK with a dictionary that forces me to use the same type for the keys and the values and that I can't tell if the values are there or not. I don't think there's a name for the practice of returning default values though; it's too trivial. – Doval Feb 25 '15 at 16:17
  • @Doval Function or dictionary, that's just style. I'm still interested in the name of this pattern. Also, who says that the value type has to be the same as the key? It's only the same by default. – Fozi Feb 25 '15 at 16:21
  • @Doval Just to clarify, the basic idea is simple. The implications are vast though. You can do much more with this than i18n while keeping your code more readable. Think aspect related programming but from a data driven approach. – Fozi Feb 25 '15 at 16:27
  • This sounds similar to a Python DefaultDict docs.python.org/2/library/… – Oliver Weiler Feb 25 '15 at 16:29
5

For the use case you've described, I think it's a valid pattern. However, there is no name for this, AFAIK.

In Java 8, there is a Map.getOrDefault method, which almost does that:

String greetingKey = "Hello";

Map<String, String> i18n = new HashMap<>();
i18n.put(greetingKey, "Hola");

String greeting = i18n.getOrDefault(greetingKey, greetingKey);

Just using a key as default value - a bit more verbose than your version, if it was implemented as a class with overridden get method.


There is more general pattern to that: "if there is no value for a requested key, return result of some function applied to a key (and other data in a map/dictionary)" - in your case it's just an identity function applied to a key. Same pattern can be used for interpolation, for example.

It may be called "interpolated map", where in your case interpolation is degenerated to just use a key (argument) and no "neighbor points" (compare: linear interpolation uses two neighbors). Yet, I've never heard of such a thing being an object-oriented pattern. It is more of a functional design pattern, which just uses higher-order functions, hence no specific name needed.


EDIT: Your approach can be considered as flawed because there is an implicit contract on map/dictionary, which states: "if containsKey returns false, then get must return null" (opposite may not hold if null is a valid value).

There probably is some code which relies on this contract, and your implementation breaks it, hence it may cause runtime errors.

  • I think it leans more towards data driven aspect oriented programming, but primary goal is code readability. If you use a generator function then you remove the language (for i18n) or resource or whatever you want to be able to slice from the code which makes it less readable as it introduces an additional level of indirection. By simply using the key=value rule you keep the initial data in place but enable replacement in the future. – Fozi Feb 25 '15 at 19:27
  • On another note, verbosity is a major concern, that's why I used the dictionary look-up because it is the least intrusive and verbose. – Fozi Feb 25 '15 at 19:28
  • @Fozi Yes, verbosity may matter, but it can be solved in other way. Added explanation of why your approach may be considered flawed. – scriptin Feb 25 '15 at 19:36
  • 1
    i18n is not a dictionary/map/hash any more and is not bound to its contracts. It is now an abstraction layer. I really think it should have a name that makes that clear. If you are right and this has no name (yet) then maybe I should come up with something. – Fozi Feb 25 '15 at 19:39
  • I'm sorry, but it is not an abstraction layer. There's no "layer" just in this single thing, and the abstraction is just the "default value". You haven't invented or discovered anything here, people use that kind of stuff every day and treat it as a special case of more general things, like higher order functions, subclassing, or interpolation. – scriptin Feb 25 '15 at 20:20
1

This doesn't sound like any Dictionary class I've heard of, but it could be trivially implemented by wrapping a dictionary in another class (or inheriting from it) and modifying the Get behavior.

  • Yes, that's what I did... I left out the code as an exercise to the reader ;) The question is - is there a name for it? – Fozi Feb 25 '15 at 16:18
  • 4
    It's called a dictionary wrapped in another class. – Robert Harvey Feb 25 '15 at 16:27
  • 1
    @Fozi - This is a programmer site. I highly encourage you to include code when possible. – JeffO Feb 25 '15 at 17:00
  • 2
    @JeffO This is not a coding question. I post coding questions on SO. The code in this case would only add noise to the question at hand. – Fozi Feb 25 '15 at 19:30
  • @Fozi - Well someone put the code in your question. – JeffO Feb 27 '15 at 2:54

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