2

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?

2
  • 1
    How about naming it "gradientToComponent" or "gradientToComponentDictionary"? (;
    – ASA
    Apr 5, 2017 at 12:36
  • 3
    I usually go with componentByGradient Apr 6, 2017 at 2:16

1 Answer 1

6

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.

1
  • 3
    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, 2017 at 17:56

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