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).
在 C# 中,我将调用执行此操作的类型 CountryCodeToNameMapping。 通常我会调用一个变量 countryCodeToNameMapping,但在某些非常有限的上下文中(例如、lambda),我可能会调用它 c 或米。
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.
地图首先不是一个集合,而是一张地图。 因此,我不会选择像 countries 或 countriesByCode 这样的名称。 根据上下文,有时强调地图的论点可能是合理的(请参阅放松的答案)。 但就我个人而言,我认为在您的情况下,
countryName = CountryCodeToName["DE"]
或类似的东西并不能反映您的意图(获得国家/地区名称)以及上面给出的名称。
I would choose
countryName = countryByCode["DE"]
unless you have a class Country in your code in which case I would choose
countryName = countryNameByCode["DE"]
It is clear, succinct, and reads easily.
A map is not first and foremost a collection, but, well, a map. Hence, I would not choose names like countries or countriesByCode. Depending on the context, it might sometimes be reasonable to emphasize the argument of the map (see unwind's answer). But personally, I think in your case,
countryName = CountryCodeToName["DE"]
or something similar does not reflect your intentions (getting a country name) as well as the name(s) given above.
发布评论
评论(8)
在这种特殊情况下,我的投票将投给
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:Looks fairly nice, should be easily understandable by anyone. Possibly change the word "code" to something more precise ("countrycode" would be my next choice).
它通过了“电话听写”测试,而且听起来也更像自然语言。
It passes the “telephone dictation” test, and also sounds more like natural language.
我喜欢用复数来表示集合。
编辑:
countryCodes
是错误的,因为您正在从代码映射到名称。I like to use plurals for collections.
Edit:
countryCodes
is wrong because you are mapping from a code to a name.发音时使用听起来正确的东西。 这也意味着适当地命名你的关键变量。 示例:
这非常有意义 - 您给
countries
一个countryCode
,它返回一个countryName
。 这将是多余的:Use something which sounds right when pronouncing it. This also means name your key variables appropriately. Example:
This makes perfect sense - you give
countries
acountryCode
, and it returns acountryName
. This would be redundant:在 C# 中,我将调用执行此操作的类型
CountryCodeToNameMapping
。 通常我会调用一个变量countryCodeToNameMapping
,但在某些非常有限的上下文中(例如、lambda),我可能会调用它c
或米
。In C#, I'd call a type that does this
CountryCodeToNameMapping
. Usually I'd call a variablecountryCodeToNameMapping
, but in certain very restricted contexts (e.g., lambdas), I'd probably call itc
orm
.我会选择,
除非你的代码中有一个类Country,在这种情况下我会选择
它是清晰、简洁且易于阅读的。
地图首先不是一个集合,而是一张地图。 因此,我不会选择像
countries
或countriesByCode
这样的名称。 根据上下文,有时强调地图的论点可能是合理的(请参阅放松的答案)。 但就我个人而言,我认为在您的情况下,或类似的东西并不能反映您的意图(获得国家/地区名称)以及上面给出的名称。
I would choose
unless you have a class Country in your code in which case I would choose
It is clear, succinct, and reads easily.
A map is not first and foremost a collection, but, well, a map. Hence, I would not choose names like
countries
orcountriesByCode
. Depending on the context, it might sometimes be reasonable to emphasize the argument of the map (see unwind's answer). But personally, I think in your case,or something similar does not reflect your intentions (getting a country name) as well as the name(s) given above.
我通常这样做:
countryCodeMappingByName
或者如果映射是唯一的,只需简单:
countryCodeMapping
I usually do it this way:
countryCodeMappingByName
Or if the mapping is unique, just simply:
countryCodeMapping
另一次投票赞成将您映射到的内容复数化。
例如。
国家=国家[code]
Another vote for just pluralizing what you're mapping to.
eg.
country = countries[code]