C# 命名约定:动词/名词和英语语法使用指南

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/412839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 02:25:08  来源:igfitidea点击:

Naming conventions: Guidelines for verbs/nouns and english grammar usage

c#.netnaming-conventions

提问by Chris S

Can anyone point me to a site, or give me some wisdom on how you go about choosing names for interfaces, classes and perhaps even methods and properties relating to what that object or method does?

谁能给我指点一个网站,或者给我一些关于如何为接口、类甚至方法和与该对象或方法的作用相关的属性选择名称的智慧?

This is specifically for Microsoft development, so Java-esque "doGet" and so on isn't really used, however some general rules that cross language barriers must (I would've thought) exist.

这是专门针对 Microsoft 开发的,因此并没有真正使用 Java 式的“doGet”等,但是必须(我认为)存在一些跨语言障碍的一般规则。

An example might help: I have 4 choices of names for an interface:

一个例子可能会有所帮助:我有 4 个接口名称选择:

IGroupedItem
IGroupableItem
IDataEntity
IGroupedEntity

They all contain an adjective and the noun, or just a noun. Looking at the .NET framework it seems like there must be some kind of ruleset for this, for consistency? Aside from the obvious verbs for methods.

它们都包含一个形容词和一个名词,或者只是一个名词。看看 .NET 框架,为了一致性,似乎必须有某种规则集?除了明显的方法动词。

Edit:Though the example is an interface, I'm not limiting this to just interfaces. The general guideline is classes are nouns, methods verbs, properties nouns. I suppose what I mean is choice of the synonym. Is throwing "Entity" everywhere wrong

编辑:虽然该示例是一个接口,但我并不仅限于接口。一般准则是类是名词,方法是动词,属性是名词。我想我的意思是同义词的选择。到处乱扔“实体”

采纳答案by Rumen Georgiev

Look at the MSDN articlesfor naming guidelines. In short:

查看MSDN 文章以获取命名指南。简而言之:

  • Use nouns for class names and property names (it's obvious)
  • For interface names, start with I and use nouns and/or adjectives to describe behavior
  • Use verbs for method names to describe action
  • 使用名词作为类名和属性名(很明显)
  • 对于接口名称,以 I 开头并使用名词和/或形容词来描述行为
  • 使用动词作为方法名称来描述动作

For your example - IGroupableItem.

对于您的示例 - IGroupableItem。

回答by Spodi

MSDN has an article just on Interface Naming Guidelinesthat may help you out. If you want the naming conventions of stuff other than interfaces, along with many other naming and design guidelines, you can find that all on MSDN, too.

MSDN 有一篇关于接口命名指南的文章可以帮助你。如果您想要接口以外的命名约定,以及许多其他命名和设计指南,您也可以在 MSDN 上找到所有内容。

回答by S.Lott

Interfaces are things a class is capable of doing. Not what it is, but what it can do.

接口是一个类能够做的事情。不是它什么,而是它可以做什么

IGroupableItem

Other names describe what things are or are too vague to be useful.

其他名称描述了哪些事物是或过于模糊而无用。

Specifically, "IDataEntity" is largely meaningless. After all, everything's a data entity.

具体来说,“IDataEntity”在很大程度上毫无意义。毕竟,一切都是数据实体。

回答by Jon Skeet

This is the same material as Spodi's answer, but MSDN's Design Guidelines for Class Library Developersare mostly excellent, covering namingand much, much more.

这与 Spodi 的答案相同,但 MSDN 的类库开发人员设计指南大多非常出色,涵盖了命名等等。

回答by Malx

There is nice article Making Wrong Code Look Wrongby Joel Spolsky. It tells about not so popular, but very handy naming convention.

乔尔·斯波尔斯基(Joel Spolsky)有一篇不错的文章 使错误的代码看起来是错误的。它讲述了不太流行但非常方便的命名约定。

回答by Nick

As well as the MSDN Guidelines, there is a C# Coding Standards document from IDesignby Juval Lowy that is quite helpful (don't know if/how much this differs from MSDN).

除了 MSDN 指南之外,还有来自Juval Lowy 的IDesign的 C# 编码标准文档,它非常有用(不知道这与 MSDN 是否/有多大不同)。

C# Coding Standards

C# 编码标准