Do you name a dictionary by the key or the value? I have a dictionary that will hold gradient data and the component:

dictionary[gradient] = component;

Do I name it gradientDictionary or componentDictionary?

closed as primarily opinion-based by gnat, Thomas Owens Apr 5 '17 at 18:20

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.

  • 1
    How about naming it "gradientToComponent" or "gradientToComponentDictionary"? (; – Traubenfuchs Apr 5 '17 at 12:36
  • I usually go with componentByGradient – Maybe_Factor Apr 6 '17 at 2:16

That depends, but generally I'd suggest naming by the values. The keys are often just a way to quickly find what you are actually looking for and will be using.

Depending on context, for your specific example, I'd also probably suggest just calling the dictionary components to avoid encoding too much type information in the name.

  • Additional, if more than one map is used to find components - rare but it happens - add a "ByFoo" suffix to distinguish them. E.g. componentsByName[name] and componentsByID[id]. It will be visually clear when the wrong one is used, as the matching parts are next to each other: componentsByName[id]. – Sjoerd Apr 5 '17 at 17:56

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