主要/进入类及其超类是否有任何命名约定?
我有一个关于 C# 中的命名约定的问题,我认为之前提出的大多数问题或常用资源(例如 MSDN 命名指南。大多数现有的命名约定都集中在框架和其他类所使用的事物上。这可能有点不同。
Main() 所在的类如何命名?它与其超类有何关系?
假设您是一名初出茅庐的程序员,并且创建了一个与此站点非常相似的小型应用程序。你可能会做这样的事情。
// the title of the class happens to be the title of the project
namespace FogCreek {
public class StackOverflow {
public static Main() { ... }
}
}
但是,随着您的成长并创建更多 Stack 站点,您决定重构您的内部结构,每个站点都复制自己的命名空间。
namespace FogCreek.StackSites.StackOverflow {
public class WhatDoICallThis : StackExchangeSite {
public static Main() { ... }
}
}
// elsewhere in another project...
namespace FogCreek {
public class StackExchangeSite {
...
}
}
我不太愿意将其称为 StackOverflow,因为它的全名是 FogCreek.StackSites.StackOverflow.StackOverflow。这不是有点多余吗?
另外,StackExchangeSite 是否应该位于 FogCreek 内自己的命名空间中?或者它应该“高于”由它衍生的项目?
Company.Foobar.Widget
-> Company.Foobar.Widget.RoundWidget
-> Company.Foobar.Widget.SquareWidget
vs
Company.Foobar.Widget.BaseWidget
-> Company.Foobar.Widget.RoundWidget
-> Company.Foobar.Widget.SquareWidget
我不喜欢第二种选择,因为我不记得任何使用“Base”一词的可扩展类。
I have a question regarding naming conventions in C# that I don't think are covered in either the majority of the previously asked questions or the usual resources like the MSDN Naming Guidelines. The majority of existing naming conventions focus on frameworks and things to be consumed by other classes. This may be a bit different.
What do you name the class where Main() resides? And how does it relate to its superclass?
Let's say you're a fledgling programmer and you create a small application much like this site. You might do something like this.
// the title of the class happens to be the title of the project
namespace FogCreek {
public class StackOverflow {
public static Main() { ... }
}
}
But then you decide to refactor your innards as you grow and create more Stack sites, each one occopying their own namespace.
namespace FogCreek.StackSites.StackOverflow {
public class WhatDoICallThis : StackExchangeSite {
public static Main() { ... }
}
}
// elsewhere in another project...
namespace FogCreek {
public class StackExchangeSite {
...
}
}
I'm hesitant to call it StackOverflow because it's full name be FogCreek.StackSites.StackOverflow.StackOverflow. Isn't that a bit redundant?
Also, should StackExchangeSite be in its own namespace within FogCreek? Or should it be "higher than" the projects that derive from it?
Company.Foobar.Widget
-> Company.Foobar.Widget.RoundWidget
-> Company.Foobar.Widget.SquareWidget
vs
Company.Foobar.Widget.BaseWidget
-> Company.Foobar.Widget.RoundWidget
-> Company.Foobar.Widget.SquareWidget
I don't favor the second option because I don't recall any extendable classes that use the word "Base".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您应该保持原样(通常称为
程序
),而优先考虑组合而不是继承。入口点是一个静态方法,因此绝对没有理由创建一个涉及持有 Main 方法的类的继承树。
I think that you should rather leave it as is (typically called
Program
) and instead favor composition over inheritance.The entry point it a static method, so there's absolutely no reason to create an inheritance tree that involves the class holding the Main method.