C# 类命名约定:是 BaseClass 还是 ClassBase 还是 AbstractClass
命名基类的推荐方法是什么? 是在类型名称前加上“Base”或“Abstract”作为前缀,还是只在它后面加上“Base”后缀?
请考虑以下内容:
类型:ViewModel
例如MainViewModel、ReportViewModel
基类:BaseViewModel
或ViewModelBase 或
AbstractViewModel
另请考虑:
类型:Product
例如 VirtualProduct、ExpiringProduct
基类:BaseProduct< /code> 或
ProductBase
或 AbstractProduct
您认为哪个更标准?
class Entity : EntityBase
{
}
或者
class Entity : BaseEntity
{
}
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"?
Consider the following:
type: ViewModel
e.g. MainViewModel, ReportViewModel
base class: BaseViewModel
or ViewModelBase
or AbstractViewModel
Also consider:
type: Product
e.g. VirtualProduct, ExpiringProduct
base class: BaseProduct
or ProductBase
or AbstractProduct
Which do you think is more standard?
class Entity : EntityBase
{
}
or
class Entity : BaseEntity
{
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我认为这是一个选择的问题。 我想说,如果您要创建大量基类,那么也许最好始终使用 BaseClassname,因为这样您总是可以通过键入 Base 并从以下位置获取其余帮助来始终找出可以开始使用的基类智能感知。 如果您有 20 个基类,并且添加了 Base 作为后缀,但您忘记了基类的名称,该怎么办? 您想首先从 VS 创建一个类图并找出可用的基类吗?
当只有一两个类时,将它们命名为 ClassBase 就可以了。
GetItems 和 ItemsGet 函数之间的决策也是如此。 我想说至少为了可读性 - 去 GetItems。 遵循约定:)
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.
Same goes for decision between GetItems and ItemsGet function. I'd say for readability's sake atleast - go for GetItems. Follow the conventions :)
就我个人而言,我建议不要添加“base”这个词。 您永远不知道什么时候必须更改代码,并且它将不再是基础对象。 话虽这么说,我们过去已经这样做了,我们在前面加上了 Base 这个词的前缀。 似乎流动性更好。
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.
BaseEntity 看起来很像驼峰式大小写 - strName、bseEntity。 我会选择 EntityBase,因为它首先定义主题,这将帮助您更快地识别它的功能。
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.
当你给东西命名时,一定要考虑按字母顺序排列。 我真的不喜欢查看 SQL 服务器,每个存储过程都被命名为 usp[something]。 同样,不要过度使用 Get 和 Set 作为函数的前导名称。 考虑将它们命名为 ItemsGet 或 OrderPlace,而不是 GetItems 或 PlaceOrder。
因此,一般来说,ClassnameBase / EntityBase 将是更好的选择。
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.
So, in general, ClassnameBase / EntityBase would be a better choice.
Framework 中有一些带有 Base 后缀的示例,例如 System.Configuration.Provider.ProviderBase、System.Web.SessionState.SessionStateStoreProviderBase。
但框架中的所有抽象基类绝不都遵循此约定(例如,
System.Data.Common.DbParameter
、System.Data.Common.DbCommand
)。就我个人而言,我会避免使用后缀除非我想强调它是一个抽象类这一事实,并且认为否则该类的用户可能希望该名称指示具体的实现。
There are examples in the Framework with the Base suffix, e.g.
System.Configuration.Provider.ProviderBase
,System.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
).Personally I would avoid using the suffix unless I 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.
以上都不是。 考虑你的基类提供什么目的; 就这么命名吧。 例如,汽车和自行车的基类可以是车辆。
如果您创建基类只是为了拥有一个类的基类,并且除此之外没有任何目的或原因,那么您可能做错了什么。
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.
如果您谈论的是虚拟基类,Microsoft 的标准是 ClassnameBase(如 CollectionBase)。
If you're talking about virtual base classes, Microsoft's standard is ClassnameBase (like CollectionBase.)
我们使用 BaseEntity,但我认为这是你自己的偏好。 我经常见到对方。
只需在您的环境中保持一致,无论是您的项目、命名空间还是您的团队(如果可能)。 恕我直言,不同的约定比糟糕的约定更糟糕。
We use BaseEntity, but I think it your own preference. I frequently see the other.
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.