What is a good naming convention for a search / hash map?

In data processing, I often need to create a search data structure to map one identifier to another. As a specific example, take a structure that contains a 1 to 1 mapping between a country code of 2 and its full name. In it we would have

AD -> Andorra AE -> United Arab Emirates AF -> Afghanistan 

What is a good name for a variable that would hold this card? Some ideas (I will use the names of the camels):

 countryNameByCode nameByCodeLookup nameCodeLookup codeToName 
+6
language-agnostic naming-conventions
source share
7 answers

My vote would be for codeToName in this particular case, and I suppose this generalizes. In order not to say that this name, I would choose myself in all cases; which is highly dependent on volume, further encapsulation, etc. But this seems like a good name that should help make your code readable:

 String country = codeToName["SV"]; 

It looks good, it should be clear to anyone. Perhaps changing the word "code" to something more precise ("countrycode" will be my next choice).

+5
source share
 country_name = countries_by_code[country_code] 

He passes the "telephone dictation" test, and also sounds more like a natural language.

+3
source share

I like to use plurals for collections.

 countryNames 

Edit: countryCodes is incorrect because you are matching code with a name.

+2
source share

I usually do this:

countryCodeMappingByName

Or, if the mapping is unique, simply:

countryCodeMapping

Rwendi

0
source share

Use what sounds correct when pronouncing. It also means the correct name for your key variables. Example:

 countryName = countries[countryCode]; 

It makes sense - you give countries a countryCode , and it returns a countryName . That would be superfluous:

 countryName = countryCodesToNames[countryCode]; 
0
source share

In C #, I would name the type that makes this CountryCodeToNameMapping . Normally I would name the variable CountryCodeToNameMapping , but in some very limited contexts (like lambdas) I would call it c or m .

0
source share

Another vote is that you are simply matching what you are cloning to.

eg. country = countries[code]

0
source share

All Articles