C# 类命名约定:是 BaseClass 还是 ClassBase 还是 AbstractClass

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

C# Class naming convention: Is it BaseClass or ClassBase or AbstractClass

c#naming-conventionsbase-class

提问by Soni Ali

What is the recommended approach to naming base classes? Is it prefixing the type name with "Base" or "Abstract" or would we just suffix it with "Base"?

命名基类的推荐方法是什么?是在类型名称前面加上“ Base”还是“ Abstract”,还是只用“Base”作为后缀?

Consider the following:

考虑以下:

type: ViewModele.g. MainViewModel, ReportViewModel

类型:ViewModel例如MainViewModelReportViewModel

base class: BaseViewModelor ViewModelBaseor AbstractViewModel

基类:BaseViewModelViewModelBaseAbstractViewModel

Also consider:

还要考虑:

type: Producte.g. VirtualProduct, ExpiringProduct

类型:Product例如VirtualProduct, ExpiringProduct

base class: BaseProductor ProductBaseor AbstractProduct

基类:BaseProductProductBaseAbstractProduct

Which do you think is more standard?

你觉得哪个更标准?

class Entity : EntityBase
{
}

or

或者

class Entity : BaseEntity
{
}

采纳答案by Joe

There are examples in the Framework with the Base suffix, e.g. System.Configuration.Provider.ProviderBase, System.Web.SessionState.SessionStateStoreProviderBase.

框架中有带有 Base 后缀的示例,例如System.Configuration.Provider.ProviderBaseSystem.Web.SessionState.SessionStateStoreProviderBase

But by no means all abstract base classes in the Framework follow this convention (e.g. System.Data.Common.DbParameter, System.Data.Common.DbCommand).

但绝非框架中的所有抽象基类都遵循此约定(例如System.Data.Common.DbParameterSystem.Data.Common.DbCommand)。

Personally I would avoid using the suffix unlessI wanted to emphasize the fact that it's an abstract class and felt that otherwise users of the class might expect the name to indicate a concrete implementation.

就我个人而言,我会避免使用后缀,除非我想强调它是一个抽象类的事实,并且认为该类的用户可能希望该名称表示具体的实现。

回答by Paul Sonier

None of the above. Consider what purpose your base class provides; name it that. For example, the base class of Automobile and Bicycle could be Vehicle.

以上都不是。考虑你的基类提供什么目的;把它命名为。例如,汽车和自行车的基类可以是车辆。

If you're creating base classes just to have a base class of one class, and with no purpose or reason other than that, you're probably doing something wrong.

如果您创建基类只是为了拥有一个类的基类,除此之外没有其他目的或原因,那么您可能做错了什么。

回答by Gary.Ray

We use BaseEntity, but I think it your own preference. I frequently see the other.

我们使用 BaseEntity,但我认为这是您自己的偏好。我经常看到另一个。

Just be consistent within your context, be that your project, namespace or if possible, your team. Different conventions are worse than a bad convention IMHO.

只需在您的上下文中保持一致,无论是您的项目、命名空间还是您的团队(如果可能)。恕我直言,不同的约定比糟糕的约定更糟糕。

回答by kemiller2002

Personally, I would recommend against adding the word base at all. You never know when you'll have to change the code around and it won't be the base object anymore. That being said, we have done this in the past, we prefixed the word Base on the front. It seems to flow better.

就个人而言,我建议完全不要添加 base 这个词。你永远不知道什么时候你必须改变代码,它不再是基础对象。话虽如此,我们过去已经这样做了,我们在前面加上了 Base 一词。似乎流动得更好。

回答by Mike Robinson

BaseEntity looks a lot like camel case - strName, bseEntity. I'd go for EntityBase since it defines the subject first, which will help you identify it's function quicker.

BaseEntity 看起来很像驼峰式样例 - strName、bseEntity。我会选择 EntityBase,因为它首先定义了主题,这将帮助您更快地识别它的功能。

回答by Yes - that Jake.

If you're talking about virtual base classes, Microsoft's standard is ClassnameBase (like CollectionBase.)

如果您在谈论虚拟基类,Microsoft 的标准是 ClassnameBase(如 CollectionBase。)

回答by marcc

Always think about alphabetizing when you name stuff. I really don't like looking at a SQL server and every stored procedure is named usp[something]. Along the same lines, don't overuse Get and Set as leading names for a function. Instead of GetItems or PlaceOrder, think about naming them as ItemsGet or OrderPlace.

当您命名事物时,请始终考虑按字母顺序排列。我真的不喜欢看 SQL 服务器,而且每个存储过程都被命名为 usp[something]。同样,不要过度使用 Get 和 Set 作为函数的前导名称。考虑将它们命名为 ItemsGet 或 OrderPlace,而不是 GetItems 或 PlaceOrder。

So, in general, ClassnameBase / EntityBase would be a better choice.

因此,总的来说,ClassnameBase / EntityBase 将是更好的选择。

回答by marcc

I think its a matter of choice. I'd say if you are creating a lot of base classes then perhaps it is better to go with BaseClassname always because that way you can ALWAYS find out what base classes you can start using by just typing Base and getting the rest of the help from Intellisense. What if you had 20 Base classes and you added Base as suffix and you forgot what was the name of the base class? Do you want to create a class diagram first from VS and find out what base classes are available? It's alright to name them ClassBase when it is just one or two classes.

我认为这是一个选择问题。我想说的是,如果您要创建很多基类,那么最好始终使用 BaseClassname ,因为这样您始终可以通过键入 Base 并从以下位置获取其余帮助来找出可以开始使用的基类智能感知。如果您有 20 个基类并且您添加了 Base 作为后缀并且您忘记了基类的名称怎么办?您想先从 VS 创建一个类图并找出可用的基类吗?当它只有一两个类时,将它们命名为 ClassBase 是可以的。

Same goes for decision between GetItems and ItemsGet function. I'd say for readability's sake atleast - go for GetItems. Follow the conventions :)

GetItems 和 ItemsGet 函数之间的决定也是如此。至少为了可读性,我会说 - 选择 GetItems。遵守约定:)