C # dictionary naming convention

What do we call a dictionary variable?

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

I know that we can create a Country class for this example. But please do not mention this alternative, because I am thinking of a good naming convention here.

+47
c # naming-conventions
Dec 02 2018-10-12T00:
source share
6 answers
 ProvincesByCountry 
+57
Dec 02 2018-10-12T00:
source share

I mainly use one of the following:

  • CountryToStatesDictionary
  • CountryToStatesMap
  • CountryToStatesMapping
+13
Dec 02 2018-10-12T00:
source share

I like XtoYMap or YFromX .

+7
Dec 02 2018-10-12T00:
source share

Naming is always contextual. Thus, in this particular case, it is advisable to indicate some name that defines the comparison of the country with the state.

if it's just a device for a loop in a wider context, and then discarded, I usually just go with the short temporary type var, like ...

 var dict = GetCountryStateMapping(); foreach(var item in dict) { //Something.... } 
+5
Dec 02 2018-10-12T00:
source share

ProvincesByCountry is not explicit enough as it sounds like a one-to-one mapping of countries to provinces. When accessing ProvincesByCountry ["Germany"], I expected that one value is an object, not a list of objects.

My personal template is similar:

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

However, if the noun describing the meaning is plural in nature, then I use postfix arrays or lists, since in English you cannot plural. I personally always stick with arrays, regardless of the actual implementation of IEnumerable or IEnumerable <T> I use, whether it be List, Array, or something else.

In your case, it refers to:

 ProvinceArraysByCountry 

Tells what it is with scientific accuracy.

I apply this rule recursively if there are dictionaries as values. The access order then goes in reverse order to the word order in the name. Imagine you are adding planets:

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

And finally, the last little touch. If such a dictionary compares the properties of the object and the objects themselves, I leave 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 template unconditionally, which is good, since it leaves absolutely no room for guesswork. Con this sometimes generates fairly long names. But I have no idea how to always have explicit, but always short names. You must always compromise. And this, of course, is a matter of taste.

This pattern does not work (or at least you break the brain) when the keys are also dictionaries, or when you have a list of dictionaries, lists of dictionaries or some other crazy exotic things. But I do not remember that there were many levels of nesting, so I am pleased with this.

+3
Jun 26 '14 at 15:38
source share

provinces, provinceMap, province

Everyone comes to mind. I like regionMap. If this is a member field, I would add the prefix "m_", as in "m_provinceMap".

+2
Dec 02 2018-10-12T00:
source share



All Articles