字典数据类型的命名约定是什么?
我们如何命名字典变量?
假设在我的方法中我有 Dictionary
,其中字典的键是国家/地区名称,值是省/州名称列表。我应该如何重命名字典
?
我知道我们可以为此示例创建一个 Country
类。但请不要提及这个替代方案,因为我在这里考虑良好的命名约定。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我主要使用其中之一:
I use mostly one of these:
ProvincesByCountry 不够明确,因为这听起来像是将国家/地区与省份一一对应。当访问 ProvincesByCountry["Germany"] 时,我预计会假设一个值是一个对象而不是对象列表。
我个人的模式是类似的:
但是,如果描述值的名词本质上是复数,那么我使用后缀 arrays 或 lists,就像在英语中一样,你可以' t 真正“复数”复数。我个人总是坚持使用数组,无论 IEnumerable 或 IEnumerable 的实际实现如何。 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:
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:
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:
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:
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.
我喜欢
XtoYMap
或YFromX
。I like
XtoYMap
orYFromX
.命名始终是上下文相关的。因此,在这种特定情况下,指定国家/地区到州的映射的名称是合适的。
如果这只是一个在更大的上下文中循环的设备然后被丢弃,我通常只使用一个短的临时类型 var,例如......
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...
省份、省份地图、省份字典
都浮现在脑海中。我喜欢我自己的省份地图。如果它是成员字段,我会添加“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".