字典数据类型的命名约定是什么?

发布于 2024-10-05 16:46:35 字数 230 浏览 5 评论 0 原文

我们如何命名字典变量?

假设在我的方法中我有 Dictionary>;字典;,其中字典的键是国家/地区名称,值是省/州名称列表。我应该如何重命名字典

我知道我们可以为此示例创建一个 Country 类。但请不要提及这个替代方案,因为我在这里考虑良好的命名约定。

How do we name a dictionary variable?

Say in my method I have Dictionary<string, List<string>> dictionary;, where the keys of the dictionary are country names and the values are lists of province/state names. How should I rename dictionary?

I know we can create a Country class for this example. But please don't mention this alternative because I'm thinking of good naming convention here.

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

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

发布评论

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

评论(6

驱逐舰岛风号 2024-10-12 16:46:35
ProvincesByCountry
ProvincesByCountry
怪异←思 2024-10-12 16:46:35

我主要使用其中之一:

  • CountryToStatesDictionary
  • CountryToStatesMap
  • CountryToStatesMapping

I use mostly one of these:

  • CountryToStatesDictionary
  • CountryToStatesMap
  • CountryToStatesMapping
最近可好 2024-10-12 16:46:35

ProvincesByCountry 不够明确,因为这听起来像是将国家/地区与省份一一对应。当访问 ProvincesByCountry["Germany"] 时,我预计会假设一个值是一个对象而不是对象列表。

我个人的模式是类似的:

[Plural of a noun describing the value]By[Singular of a noun describing the key]

但是,如果描述值的名词本质上是复数,那么我使用后缀 arrayslists,就像在英语中一样,你可以' t 真正“复数”复数。我个人总是坚持使用数组,无论 IEnumerableIEnumerable 的实际实现如何。 T>我正在使用,列表,或数组或其他。

在你的例子中,它变成:

ProvinceArraysByCountry

以科学的精确度告诉它是什么。

如果有字典作为值,我会递归地应用此规则。然后,访问顺序与名称中的单词顺序相反。想象一下你添加了行星:

ProvinceArraysByCountryByPlanet["Earth"]["Germany"][0] = "Bavaria"
ProvinceArraysByCountryByPlanet["Earth"]["Germany"][1] = "Rhineland-Palatinate"

最后是最后一个小笔划。如果这样的字典映射对象属性和对象本身,那么我会在关键部分中省略描述对象的单词。我的意思是:

NodesByIndex[node.Index] = node; // - Do
NodesByNodeIndex[node.Index] = node; // - Don't

我无条件地使用这种模式,这很好,因为它绝对没有留下猜测的空间。缺点是它有时会生成相当长的名称。但我不知道如何拥有始终明确但始终简短的名称。你总是必须妥协。这当然是一个品味问题。

当键也是字典或者当你有字典列表的字典列表或其他一些疯狂的异国情调的东西时,这种模式不起作用(或者至少你会打破你的大脑)。但我不记得有那么多级别的嵌套,所以我对此很满意。

ProvincesByCountry is not explicit enough, as it sounds like mapping countries to provinces one to one. When accessing ProvincesByCountry["Germany"] I'd expectedly assume one value is an object rather than a list of objects.

My personal pattern is similar:

[Plural of a noun describing the value]By[Singular of a noun describing the key]

However, if a noun describing the value is plural by its nature, then I use the postfix arrays, or lists, as in English you can't really "pluralise" a plural. I personally always stick to arrays, regardless of the actual implementation of IEnumerable or IEnumerable< T> I'm using, be that List, or Array or whatever.

In your case it turns to:

ProvinceArraysByCountry

Tells what it is with scientific precision.

I apply this rule recursively if there are dictionaries as values. The order of accessing then goes in reverse to the order of words in the name. Imagine you add planets:

ProvinceArraysByCountryByPlanet["Earth"]["Germany"][0] = "Bavaria"
ProvinceArraysByCountryByPlanet["Earth"]["Germany"][1] = "Rhineland-Palatinate"

And finally the last little stroke here. If such dictionary maps object properties and the objects themselves, then I leave out the word describing the object in the key section. Here is what I mean:

NodesByIndex[node.Index] = node; // - Do
NodesByNodeIndex[node.Index] = node; // - Don't

I use this pattern unconditionally which is good as it leaves absolutely no room for guess. Con is it generates fairly long names sometimes. But I have no idea how to have always explicit but always short names. You always have to compromise. And it's of course a matter of taste.

This pattern doesn't work (or at least you'd break your brain) when keys are also dictionaries or when you have list of dictionaries of lists of dictionaries or some other crazy exotic stuff. But I don't remember having that many levels of nesting, so I'm happy with it.

桃气十足 2024-10-12 16:46:35

我喜欢 XtoYMapYFromX

I like XtoYMap or YFromX.

自控 2024-10-12 16:46:35

命名始终是上下文相关的。因此,在这种特定情况下,指定国家/地区到州的映射的名称是合适的。

如果这只是一个在更大的上下文中循环的设备然后被丢弃,我通常只使用一个短的临时类型 var,例如......

var dict = GetCountryStateMapping();
foreach(var item in dict)
{
  //Something....
}

Naming is always contextual. So in this specific case some name specifying the country to state mapping is appropriate.

if this was just a device for a loop within a larger context and then discarded, I normally just go with a short temp type var like...

var dict = GetCountryStateMapping();
foreach(var item in dict)
{
  //Something....
}
对不⑦ 2024-10-12 16:46:35

省份、省份地图、省份字典

都浮现在脑海中。我喜欢我自己的省份地图。如果它是成员字段,我会添加“m_”前缀,如“m_provinceMap”中所示。

provinces, provinceMap, provinceDictionary

All come to mind. I like provinceMap my self. If it's a member field I would add an "m_" prefix, as in "m_provinceMap".

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