80

How do we name a dictionary variable?

Say in my method I have Dictionary<string, List<string>> dictionary;, where the keys of the dictionary are country names and the values are lists of province/state names. How should I rename dictionary?

I know we can create a Country class for this example. But please don't mention this alternative because I'm thinking of good naming convention here.

wonea
  • 4,783
  • 17
  • 86
  • 139
Shuo
  • 4,749
  • 9
  • 45
  • 63
  • 1
    You can think of a dictionary as a mapping function or you can think of it more specifically as a hashing/lookup/index function. Mapping is more general. Consider a math function that maps Celsius to Fareheit which could be named ConvertCelsiusToFarenheit. "To" emphasizes that the input space/set is the focus. Y-By-X better captures that a dictionary is a specific type of mapping that uses set-based hashing. "By" emphasizes that the output space/set is the focus. A key is worthless unless it unlocks something. Just my opinion, but I hope it helps give some insight. – VoteCoffee Dec 22 '14 at 13:53
  • Another quick note, you want to capture what it is for, not what what it does: a real dictionary is for looking up definitions by words. It's true that it maps some words to their definitions, but this doesn't capture intent as well. – VoteCoffee Dec 22 '14 at 13:58

6 Answers6

127
ProvincesByCountry
Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • Is there some reference which says ValueByKey is the standard for naming dictionaries in C#? It seems like there are many equally good conventions for naming dictionaries. Did the C# community just settle into ValueByKey without any canonical reference (perhaps following Microsoft's lead)? – Daniel Koverman Dec 09 '14 at 17:40
  • 3
    It's not an established convention, as far as I know. It just seemed like a good and concise solution for the case at hand. – Heinzi Dec 11 '14 at 06:40
  • It's strange to see Lou Franco's answer have 5 votes where yours has 29. It seems like you both picked "good and concise" solutions, so I assumed yours had to be more right somehow. I guess your answer just got the momentum (or perhaps better addressed the fact that "Provinces" is plural). – Daniel Koverman Dec 11 '14 at 07:14
  • 2
    I would think it's because FooByBar makes appending Map or Dictionary to the name redundant. I think the difference between By and To is By means you are mapping an id/key/field of something to the thing itself, whereas To means you are mapping two similarly complex objects to each other. If the collection is like a Lookup but with only one value, then I'd use By. If the collection resembles a linguistic dictionary then I'd use To. – Novaterata Apr 02 '15 at 14:16
  • 1
    thanks for this. slick solution. I was dreading ending it with `dictionary`/`Map` and this is perfect. – basher Oct 06 '16 at 15:48
  • This is the clearest solution. It's rarely a good idea to include container types in variable names as the information is superfluous, offering us no more insight into the variable's meaning. – sookie May 04 '18 at 11:39
  • 1
    This comes from sql server naming conventions FYI, which is a natural reference for naming conventions of collections. Other alternatives would be CountryToProvince, or something more explicit of the relationship that the dictionary represents, such as ProvinceInCountry, CountryOfProvince, etc. It's also better to use the singular form of the key/value entities. – VoteCoffee Nov 04 '19 at 15:07
  • 3
    In some cases, we can use `Per` instead of `By`. Ex: `RulesPerTenant` – user1234 Apr 08 '21 at 20:33
25

I use mostly one of these:

  • CountryToStatesDictionary
  • CountryToStatesMap
  • CountryToStatesMapping
herzmeister
  • 11,101
  • 2
  • 41
  • 51
9

ProvincesByCountry is not explicit enough, as it sounds like mapping countries to provinces one to one. When accessing ProvincesByCountry["Germany"] I'd expectedly assume one value is an object rather than a list of objects.

My personal pattern is similar:

[Plural of a noun describing the value]By[Singular of a noun describing the key]

However, if a noun describing the value is plural by its nature, then I use the postfix arrays, or lists, as in English you can't really "pluralise" a plural. I personally always stick to arrays, regardless of the actual implementation of IEnumerable or IEnumerable< T> I'm using, be that List, or Array or whatever.

In your case it turns to:

ProvinceArraysByCountry

Tells what it is with scientific precision.

I apply this rule recursively if there are dictionaries as values. The order of accessing then goes in reverse to the order of words in the name. Imagine you add planets:

ProvinceArraysByCountryByPlanet["Earth"]["Germany"][0] = "Bavaria"
ProvinceArraysByCountryByPlanet["Earth"]["Germany"][1] = "Rhineland-Palatinate"

And finally the last little stroke here. If such dictionary maps object properties and the objects themselves, then I leave out the word describing the object in the key section. Here is what I mean:

NodesByIndex[node.Index] = node; // - Do
NodesByNodeIndex[node.Index] = node; // - Don't

I use this pattern unconditionally which is good as it leaves absolutely no room for guess. Con is it generates fairly long names sometimes. But I have no idea how to have always explicit but always short names. You always have to compromise. And it's of course a matter of taste.

This pattern doesn't work (or at least you'd break your brain) when keys are also dictionaries or when you have list of dictionaries of lists of dictionaries or some other crazy exotic stuff. But I don't remember having that many levels of nesting, so I'm happy with it.

Stepan Stulov
  • 161
  • 1
  • 5
  • code completion and wide monitors have been a great boon to meaningful variable names – DMac the Destroyer Apr 02 '15 at 20:39
  • 3
    As I have seen in Microsoft's source code they use the `Collection` postfix. So I would say `ProvinceCollectionsByCountry` or maybe a more specialized name: `ProvinceCollectionsByCountryName`. – Gabor Feb 08 '17 at 15:14
8

I like XtoYMap or YFromX.

Lou Franco
  • 87,846
  • 14
  • 132
  • 192
4

Naming is always contextual. So in this specific case some name specifying the country to state mapping is appropriate.

if this was just a device for a loop within a larger context and then discarded, I normally just go with a short temp type var like...

var dict = GetCountryStateMapping();
foreach(var item in dict)
{
  //Something....
}
Tim Jarvis
  • 18,465
  • 9
  • 55
  • 92
  • 15
    Reminds me of a IT joke... "There are only 2 hard problems in computer science, naming things, cache invalidation and Off by 1 errors." – Tim Jarvis Dec 02 '10 at 01:05
  • OK. But what if I want this be used as a property? – Shuo Dec 02 '10 at 01:08
  • 2
    Ah, then in that case you have a context and a scope, the parent class (along with the contents) and its lifetime. In that case Naming is much easier. As I said, naming is always contextual. In that case, RegionByCountry or StateByCountry or ProvinceByCountry etc would be most appropriate IMO. – Tim Jarvis Dec 02 '10 at 01:13
2

provinces, provinceMap, provinceDictionary

All come to mind. I like provinceMap my self. If it's a member field I would add an "m_" prefix, as in "m_provinceMap".

Scott Wisniewski
  • 24,561
  • 8
  • 60
  • 89
  • 1
    Ok. but this doesn't reflect the keys. – Shuo Dec 02 '10 at 00:59
  • I'm ok with that. It should be pretty implicit in this case. I would go for shorter names, unless there's a reason that the keys could be ambiguous. If you had multiple maps containing provinces, then differentiating the keys would be important. In your example, thoug, there's no ambiguity. – Scott Wisniewski Dec 02 '10 at 01:03
  • 1
    Hmm... I'm OK with provinceMap, provinceDictionary. But provinces seems to be a name of a List. – Shuo Dec 02 '10 at 01:11