如何避免疯狂的命名约定?
通常将程序集命名为一个名称,将程序集内的文件夹命名为另一个名称,然后 开始将这些名称带入这些文件夹内的类中?例如:
Project.Presenter
Carriers
CarriersFindPreferedCarriersPresenter.cs
CarriersPreferencesPresenter.cs
PreferredTargetLocationPresenter.cs
Project.Service.Fixture
Carriers
CarriersServiceFixture.cs
或者更进一步,甚至是这样的方法:
List<PreferredTargetLocationPresenter.PreferredTargetLocation>
newlyAddedPreferredLocations = new
List<PreferredTargetLocationPresenter.PreferredTargetLocation>();
newlyAddedPreferredLocations.add(destinationLocation.PreferredCity);
对我来说,随着您在项目上工作的时间越来越长并开始添加其他程序集和方法,这似乎变得越来越令人困惑。有更好的方法来处理这个问题吗?
欢迎任何反馈。
Is it common to name an assembly one name, name a folder inside of the assembly another and then
start to carry those names into the classes inside of those folders? For example:
Project.Presenter
Carriers
CarriersFindPreferedCarriersPresenter.cs
CarriersPreferencesPresenter.cs
PreferredTargetLocationPresenter.cs
Project.Service.Fixture
Carriers
CarriersServiceFixture.cs
Or to carry this futher, even methods such as this:
List<PreferredTargetLocationPresenter.PreferredTargetLocation>
newlyAddedPreferredLocations = new
List<PreferredTargetLocationPresenter.PreferredTargetLocation>();
newlyAddedPreferredLocations.add(destinationLocation.PreferredCity);
To me this seems to grow more and more confusing as you work on a project longer and start to add additional assemblies and methods. Is there a better way to work with this?
Any feedback would be welcomed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
务实的程序员普及了 DRY 原则:不要重复自己。这也适用于命名。一次又一次重复相同的范围名称或前缀不会添加任何更多信息,只会使名称变得更长、可读性更差、更容易输入错误并且更难以搜索。如果您有 100 个以
PreferredLocation*
开头的类名,那么您将很难找到正确的一个:-(所以我完全反对这一点。类和方法名称的范围由封闭路径决定/ 项目名称(在 java 中是
package
,在 C# 中我不知道正确的术语是什么),所以如果您需要有关类/方法的下落的所有信息,就足够了看看它的完全限定名称。但是,在常规代码中,不应强制在任何地方都使用完全限定名称,唯一的例外是名称冲突,但我认为应该将其视为例外而不是规则。此外,在设计良好的应用程序中,大多数方法/类不是全局可见的,仅在各自的包内可见(编程语言允许这样做 - Java 允许,我相信 C# 也是如此),这减少了名称冲突的风险。并进一步消除了对类名前缀的需要。
The Pragmatic Programmers popularized the DRY principle: Don't Repeat Yourself. This applies to naming too. Repeating the same scope names or prefixes again and again does not add any more information, just makes the names longer, less readable, easier to mistype and harder to search for. If you have 100 class names starting with
PreferredLocation*
, you are going to have a hard time finding the right one :-(So I am fully against this. Class and method names are scoped by the enclosing path / project names (in java that would be
package
, in C# I don't know what the proper term is), so if you need all info about the whereabouts of a class/method, it's enough to look at the fully qualified name of it. However, in regular code one should not be forced to use the fully qualified name everywhere. The only exception is of name clashes, but I believe that should be treated as an exception rather than the rule.Moreover, in a well designed app, most of the methods / classes are not globally visible, only inside their respective package (where the programming language allows this - Java does, I am sure that C# too). This lessens the risk of name clashes and obviates the need for class name prefixes even further.
问一百个不同的人这个问题,你会得到一百个不同的答案。我喜欢任何使编写/维护代码最简单的方法,其中一半是长描述性名称,另一半是简短而甜蜜的名称。只要代码直观且灵活,我看不出任何一种方式有问题。
Ask a hundred different people this question, and you'll get a hundred different answers. I'm a fan of whatever method makes writing/maintaining the code the simplest, which is long descriptive names half the time, and short and sweet names the other half. As long as the code is intuitive and flexible, I can't see a problem with either way.
有时您必须使用较长的名称,但通常希望名称尽可能短。关键是要有描述性的名称,提供足够的细节,仅此而已。
Sometimes you'll have to use longer names, but you generally want to keep names as short as possible. The key is to have descriptive names, that give enough detail and no more.
PreferredTargetLocationPresenter.PreferredTargetLocation
是PreferredTargetLocationPresenter
类型的子类型吗?换句话说,你是嵌套类吗?如果是这样,您最好将
PreferredTargetLocation
分解到它自己的类中。这允许您编写:而不是
如果您在 C# 3.0 中工作,您可以使用
var
进一步缩短声明:Is
PreferredTargetLocationPresenter.PreferredTargetLocation
a subtype of thePreferredTargetLocationPresenter
type? In other words, are you nesting classes?If so, you might be better off breaking out
PreferredTargetLocation
into its own class. This allows you to write:instead of
If you are working in C# 3.0, you can shorten the declaration further by using
var
: