views:

179

answers:

7

In data processing, I frequently need to create a lookup data structure to map one identifier to another. As a concrete example, let's take a structure which holds a 1-to-1 mapping between a country's 2 character code and its full name. In it we would have

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

What's a good name for the variable that would hold this map? Some ideas (I'll use camel-case names):

countryNameByCode
nameByCodeLookup
nameCodeLookup
codeToName
A: 

I usually do it this way:

countryCodeMappingByName

Or if the mapping is unique, just simply:

countryCodeMapping

RWendi

RWendi
really? I can't imagine having to type that every time I use that hashmap.
Claudiu
I know its a bit long for a variable name, but its not ambigious, which I think is important...
RWendi
+3  A: 

My vote would be for codeToName in this particular case, and I guess that generalizes. That's not to say that it's the name I would have chosen myself in all cases; that depends a lot on scope, further encapsulation, and so on. But it feels like a good name, that should help make your code readable:

String country = codeToName["SV"];

Looks fairly nice, should be easily understandable by anyone. Possibly change the word "code" to something more precise ("countrycode" would be my next choice).

unwind
+2  A: 

I like to use plurals for collections.

countryNames

Edit: countryCodes is wrong because you are mapping from a code to a name.

eed3si9n
Too bad Joel hasn't added a "revoke answer" feature yet.
trenton
A: 

Use something which sounds right when pronouncing it. This also means name your key variables appropriately. Example:

countryName = countries[countryCode];

This makes perfect sense - you give countries a countryCode, and it returns a countryName. This would be redundant:

countryName = countryCodesToNames[countryCode];
Claudiu
A: 

In C#, I'd call a type that does this CountryCodeToNameMapping. Usually I'd call a variable countryCodeToNameMapping, but in certain very restricted contexts (e.g., lambdas), I'd probably call it c or m.

Jay Bazuzi
+2  A: 
country_name = countries_by_code[country_code]

It passes the “telephone dictation” test, and also sounds more like natural language.

ΤΖΩΤΖΙΟΥ
A: 

Another vote for just pluralizing what you're mapping to.

eg. country = countries[code]

albertb