惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - Anders Cui

jieba.NET与Lucene.Net的集成 jieba中文分词的.NET版本:jieba.NET 迷人的斐波那契数 - Anders Cui 趣题一则:交替放置的碟子 趣题一则:冯·诺依曼邻居问题 - Anders Cui 趣题一则:寻找那扇门 HashSet的实现(下) HashSet的实现(上) 趣题一则:如何快速过桥? 制作自己的wikibook 玩儿一下Word Search Puzzle 整数的展开 离散数学拾趣(三):集合的子集有多少个 - Anders Cui 找零钱的两种方法 离散数学拾趣(二):逻辑难题 离散数学拾趣(一) 扩展方法浅谈 意外之喜 最近遭遇的两个VS配置
关于命名中的数量和人称
Anders Cui · 2011-01-26 · via 博客园 - Anders Cui

关于命名中的数量和人称

2011-01-26 00:16  Anders Cui  阅读(3042)  评论()    收藏  举报

我们都明白命名的重要性,如果对命名不断地关注,就需要考虑命名中的动词和名词,特别的情况是动词的人称和名词的数量。

在.NET Framework的Guideline中,有专门一章讨论了命名规范,包括大小写、单词的选择等,却没有涉及到人称和数量,下面来讨论一下这两方面的内容。

名词的数量

首先来看数量。名词的单数和复数在语义上有着明显的不同,为了提高可读性,数量需要认真地考虑。比如:

  • 属性(Property):System.Collections.Generic.Dictionary<TKey, TValue>中的Keys和Count是好的例子。字段与此类似;
  • 方法名中的名词:在JS中我们熟悉的getElementById和getElementsByTagName;
  • 局部变量:与属性相比,我们更可能遭遇的是局部变量。比如,用变量来表示一个人的名字,可以用string name; 如果是多个人的名字,则可以用List<string> names。

一般地,单数表达的语义是?(0或1);复数表达的语义是*(0、1或多个)。关于名词的数量很容易理解,相关的规则也容易遵循。

动词的人称

下面再来考虑动词的人称。在需要考虑命名的对象中,包括命名空间、类型、各种类型成员,只有方法是动词,语义上表示一个行为。关于它的人称,来看一个.NET Framework中的例子。在System.Collections.Generic.Stack<T>中,有一个Contains(T item)方法,对于这样的代码:

if (theStack.Contains(1))
{
    // Do something.
}

读起来像是:if the stack contains 1,比较通顺。是不是所有方法都要这样命名呢?没有。另两个方法Clear()和Push(T item)就不是如此,这个地方很让人困惑。先写成代码看看:

theStack.Push(3);
theStack.Clear();

尝试像上面那样作为一个句子来读:the stack push 3; the stack clear,语法上不对,看起来也应该是第三人称。这里我也不确定为何如此,只能尝试来解释下。考察多个集合类型和其它类型后发现,所有使用第三人称的地方都是谓词函数,除了上面的Contains(T item),还有Directory.Exists(string path),不知道这是不是其中的命名规则呢?

还要考虑注释

一定程度上,注释也算是一种代码。一种是XML文档注释,在.NET Framework中,不管是对于类型还是类型成员,都使用了第三人称,这一点我们也可以遵循。其它的普通注释应该也可以遵循这个规则。