.net 抽象类的命名约定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/429470/
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-09-03 10:57:39  来源:igfitidea点击:

Naming conventions for abstract classes

.netnaming-conventionsabstract-class

提问by Mike Hofer

I distinctly remember that, at one time, the guideline pushed by Microsoft was to add the "Base" suffix to an abstract class to obviate the fact that it was abstract. Hence, we have classes like System.Web.Hosting.VirtualFileBase, System.Configuration.ConfigurationValidatorBase, System.Windows.Forms.ButtonBase, and, of course, System.Collections.CollectionBase.

我清楚地记得,有一次,微软推动的指导方针是在抽象类中添加“Base”后缀,以消除它是抽象的这一事实。因此,我们有像System.Web.Hosting.VirtualFileBase, System.Configuration.ConfigurationValidatorBase, System.Windows.Forms.ButtonBase, 当然还有System.Collections.CollectionBase.

But I've noticed that, of late, a lot of abstract classes in the Framework don't seem to be following this convention. For example, the following classes are all abstract but don't follow this convention:

但我注意到,最近,框架中的许多抽象类似乎没有遵循这个约定。例如,以下类都是抽象的,但不遵循此约定:

  • System.DirectoryServices.ActiveDirectory.DirectoryServer

  • System.Configuration.ConfigurationElement

  • System.Drawing.Brush

  • System.Windows.Forms.CommonDialog

  • System.DirectoryServices.ActiveDirectory.DirectoryServer

  • System.Configuration.ConfigurationElement

  • System.Drawing.Brush

  • System.Windows.Forms.CommonDialog

And that's just what I could drum up in a few seconds. So I went looking up what the official documentation had to say, to make sure I wasn't crazy. I found the Names of Classes, Structs, and Interfaceson MSDN at Design Guidelines for Developing Class Libraries. Oddly, I can find no mention of the guideline to add "Base" to the end of an abstract class's name. And the guidelines are no longer available for version 1.1 of the Framework.

这就是我可以在几秒钟内鼓起来的。所以我去找官方文档说了什么,以确保我没有疯。我在 MSDN 上的类库开发设计指南中找到了类、结构和接口名称。奇怪的是,我找不到将“Base”添加到抽象类名称末尾的指南。并且指南不再适用于框架的 1.1 版。

So, am I losing it? Did this guideline ever exist? Has it just been abandoned without a word? Have I been creating long class names all by myself for the last two years for nothing?

所以,我输了吗?这个指南曾经存在过吗?是不是一言不发就被抛弃了?在过去的两年里,我是否一直在自己创建很长的班级名称?

Someone throw me a bone here.

有人在这里扔给我一根骨头。

UpdateI'm not crazy. The guideline existed. Krzysztof Cwalina gripes about it in 2005.

更新我没疯。该指南存在。Krzysztof Cwalina 在 2005 年抱怨它。

采纳答案by Iain Holder

In Framework Design Guidelinesp 174 states:

框架设计指南第 174 页中指出:

Avoidnaming base classes with a "Base" suffix if the class is intended for use in public APIs.

如果类旨在用于公共 API,请避免使用“Base”后缀命名基类。

Also : http://blogs.msdn.com/kcwalina/archive/2005/12/16/BaseSuffix.aspx

另外:http: //blogs.msdn.com/kcwalina/archive/2005/12/16/BaseSuffix.aspx

回答by Joel Coehoorn

Also, if the abstract class has a few static members that will be used the 'Base' can get ugly.

此外,如果抽象类有一些将使用的静态成员,则“Base”可能会变得丑陋。

回答by Mehrdad Afshari

I don't remember such a guideline. I believe you should use the naming that makes sense. Sometimes the abstract class is only designed to provide common functionality to some classes (as a tool), which I think should have the suffix. However, in some cases, you want to use it as the base of a polymorphism hierarchy which it's not complete itself. In those cases I suggest naming like a normal class.

我不记得有这样的指导方针。我相信你应该使用有意义的命名。有时抽象类只是设计来为某些类提供通用功能(作为工具),我认为应该有后缀。但是,在某些情况下,您希望将其用作多态层次结构的基础,而它本身并不完整。在这些情况下,我建议像普通类一样命名。

As you see, you won't probably declare a method that accepts a ButtonBase as parameter. It's designed to provide minimal functionality for subclasses. However, you might treat a ConfigurationElementas an entity that has different forms but it is not complete on itself (and hence it's abstract)

如您所见,您可能不会声明接受 ButtonBase 作为参数的方法。它旨在为子类提供最少的功能。但是,您可能会将 aConfigurationElement视为具有不同形式但本身并不完整的实体(因此它是抽象的)

回答by James

Sometimes Base is still necessary, especially when you provide both a concrete class and an abstract class for someone to extend to create a concrete implementation.
e.g. Controller and ControllerBase (actually Controller is also abstract, but provides signifigantly more functionality than ControllerBase)

有时 Base 仍然是必要的,尤其是当您同时提供具体类和抽象类供某人扩展以创建具体实现时。
例如 Controller 和 ControllerBase(实际上 Controller 也是抽象的,但提供的功能比 ControllerBase 多得多)

Base suffix is ugly when programming against an interface, so I think the Microsoft guideline not to use it applies when the abstract class is predominantly used like an interface. Probably what they mean by Public API.

当针对接口编程时,基本后缀很难看,所以我认为微软的指导方针是在抽象类主要像接口一样使用时不使用它。可能他们所说的公共 API 是什么意思。

The point is that there are cases where there is no better alternative to using the Base suffix.

关键是在某些情况下没有更好的替代方法来使用 Base 后缀。

回答by Kooooons

I understand the inclination to avoid a Base-Suffix, but I also understand the need for someSuffix. Now, a Comment of this articlesuggests using "Type" as a Suffix as second choice to not using any. I believe this to be confusing, but the Idea that "such a non-committal word would tend to indicate that it's a non-committed class" stuck with me.

我理解避免使用基本后缀的倾向,但我也理解需要一些后缀。现在,这篇文章的评论建议使用“类型”作为后缀作为不使用任何的第二选择。我认为这令人困惑,但“这样一个非承诺性的词往往表明它是一个非承诺类”的想法一直困扰着我。

As an Alternative:I'd prefer using "Kind" as a suffix to state the object as “of or belonging to a specified race or family” (Wiktionary: -kind).

作为替代方案:我更喜欢使用“种类”作为后缀来说明对象“属于或属于特定种族或家庭”(维基词典:-kind)。

Example: DataProviderand ReflectiveDataProviderare both DataProviderKind

例子:DataProvider而且ReflectiveDataProvider都是DataProviderKind

Inspired by Biology where e.g. "canis lupus" belongs to the family "Canoidea", which very roughly translates to "dog-ish".

受到生物学的启发,其中例如“犬狼疮”属于“犬科动物”,大致翻译为“犬类”。

回答by Sam Jazz

Microsoft states, at:

微软声明,在:

https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/names-of-classes-structs-and-interfaces

https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/names-of-classes-structs-and-interfaces

"? CONSIDER ending the name of derived classes with the name of the base class. This is very readable and explains the relationship clearly. Some examples of this in code are: ArgumentOutOfRangeException, which is a kind of Exception, and SerializableAttribute, which is a kind of Attribute. However, it is important to use reasonable judgment in applying this guideline; for example, the Button class is a kind of Control event, although Control doesn't appear in its name."

“?考虑以基类的名称结束派生类的名称。这非常易读并且清楚地解释了关系。代码中的一些示例是:ArgumentOutOfRangeException,它是一种异常,和 SerializableAttribute,它是一个属性的种类。但是,在应用此准则时使用合理的判断很重要;例如,Button 类是一种 Control 事件,尽管 Control 没有出现在其名称中。

Generally speaking, this implicitly rules out using "Base" in the name.

一般来说,这隐含地排除了在名称中使用“Base”的可能性。