Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's a good naming convention for a lookup map/hash? [closed]

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
like image 482
Trenton Avatar asked Oct 20 '08 05:10

Trenton


3 Answers

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).

like image 144
unwind Avatar answered Sep 28 '22 02:09

unwind


country_name = countries_by_code[country_code]

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

like image 40
tzot Avatar answered Sep 28 '22 01:09

tzot


I like to use plurals for collections.

countryNames

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

like image 38
Eugene Yokota Avatar answered Sep 28 '22 02:09

Eugene Yokota