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

发布于 2024-07-08 14:15:16 字数 1450 浏览 4 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

轮廓§ 2024-07-15 14:15:16

在这种特殊情况下,我的投票将投给 codeToName,我想这具有普遍性。 这并不是说在任何情况下我都会选择这个名字; 这在很大程度上取决于范围、进一步的封装等等。 但这感觉是一个好名字,应该有助于提高代码的可读性:

String country = codeToName["SV"];

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

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

只为守护你 2024-07-15 14:15:16
country_name = countries_by_code[country_code]

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

country_name = countries_by_code[country_code]

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

南街女流氓 2024-07-15 14:15:16

我喜欢用复数来表示集合。

countryNames

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

I like to use plurals for collections.

countryNames

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

猫性小仙女 2024-07-15 14:15:16

发音时使用听起来正确的东西。 这也意味着适当地命名你的关键变量。 示例:

countryName = countries[countryCode];

这非常有意义 - 您给 countries 一个 countryCode,它返回一个 countryName。 这将是多余的:

countryName = countryCodesToNames[countryCode];

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];
心在旅行 2024-07-15 14:15:16

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

骷髅 2024-07-15 14:15:16

我会选择,

countryName = countryByCode["DE"]

除非你的代码中有一个类Country,在这种情况下我会选择

countryName = countryNameByCode["DE"]

它是清晰、简洁且易于阅读的。

地图首先不是一个集合,而是一张地图。 因此,我不会选择像 countriescountriesByCode 这样的名称。 根据上下文,有时强调地图的论点可能是合理的(请参阅放松的答案)。 但就我个人而言,我认为在您的情况下,

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.

烂柯人 2024-07-15 14:15:16

我通常这样做:

countryCodeMappingByName

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

countryCodeMapping

I usually do it this way:

countryCodeMappingByName

Or if the mapping is unique, just simply:

countryCodeMapping

淑女气质 2024-07-15 14:15:16

另一次投票赞成将您映射到的内容复数化。

例如。 国家=国家[code]

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

eg. country = countries[code]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文