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
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).
country_name = countries_by_code[country_code]
It passes the “telephone dictation” test, and also sounds more like natural language.
I like to use plurals for collections.
countryNames
Edit: countryCodes
is wrong because you are mapping from a code to a name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With