查找映射/哈希的命名约定是什么?

时间:2022-01-23 23:31:37

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

在数据处理中,我经常需要创建一个查找数据结构来将一个标识符映射到另一个标识符。作为一个具体的例子,让我们采用一个结构,该结构在一个国家的2个字符代码和它的全名之间保持一对一的映射。我们会在其中

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

7 个解决方案

#1


5  

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:

在这种特殊情况下,我的投票将是针对codeToName的,我想这是概括的。这并不是说这是我在所有情况下都会选择的名字;这在很大程度上取决于范围,进一步的封装等等。但它感觉就像一个好名字,应该有助于使您的代码可读:

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

看起来相当不错,任何人都应该容易理解。可能会将“代码”一词更改为更精确的内容(“国家代码”将是我的下一个选择)。

#2


3  

country_name = countries_by_code[country_code]

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

它通过了“电话听写”测试,听起来更像是自然语言。

#3


2  

I like to use plurals for collections.

我喜欢使用复数形式的集合。

countryNames

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

编辑:countryCodes是错误的,因为您正在从代码映射到名称。

#4


0  

I usually do it this way:

我通常这样做:

countryCodeMappingByName

Or if the mapping is unique, just simply:

或者,如果映射是唯一的,只需:

countryCodeMapping

RWendi

#5


0  

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:

这非常有意义 - 您为国家/地区提供countryCode,并返回countryName。这将是多余的:

countryName = countryCodesToNames[countryCode];

#6


0  

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.

在C#中,我将调用一个执行此CountryCodeToNameMapping的类型。通常我会调用一个变量countryCodeToNameMapping,但在某些非常有限的上下文中(例如,lambdas),我可能称之为c或m。

#7


0  

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

另一个投票只是为了复制你所映射的内容。

eg. country = countries[code]

例如。 country = countries [code]

#1


5  

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:

在这种特殊情况下,我的投票将是针对codeToName的,我想这是概括的。这并不是说这是我在所有情况下都会选择的名字;这在很大程度上取决于范围,进一步的封装等等。但它感觉就像一个好名字,应该有助于使您的代码可读:

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

看起来相当不错,任何人都应该容易理解。可能会将“代码”一词更改为更精确的内容(“国家代码”将是我的下一个选择)。

#2


3  

country_name = countries_by_code[country_code]

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

它通过了“电话听写”测试,听起来更像是自然语言。

#3


2  

I like to use plurals for collections.

我喜欢使用复数形式的集合。

countryNames

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

编辑:countryCodes是错误的,因为您正在从代码映射到名称。

#4


0  

I usually do it this way:

我通常这样做:

countryCodeMappingByName

Or if the mapping is unique, just simply:

或者,如果映射是唯一的,只需:

countryCodeMapping

RWendi

#5


0  

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:

这非常有意义 - 您为国家/地区提供countryCode,并返回countryName。这将是多余的:

countryName = countryCodesToNames[countryCode];

#6


0  

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.

在C#中,我将调用一个执行此CountryCodeToNameMapping的类型。通常我会调用一个变量countryCodeToNameMapping,但在某些非常有限的上下文中(例如,lambdas),我可能称之为c或m。

#7


0  

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

另一个投票只是为了复制你所映射的内容。

eg. country = countries[code]

例如。 country = countries [code]