4

将程序集命名为一个名称,将程序集内的文件夹命名为另一个名称,然后开始将这些名称携带到这些文件夹内的类中是否常见?例如:

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);        

对我来说,随着您在项目上工作的时间更长并开始添加其他程序集和方法,这似乎变得越来越令人困惑。有没有更好的方法来解决这个问题?

任何反馈都将受到欢迎。

4

4 回答 4

4

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.

于 2010-02-09T21:00:18.240 回答
4

问一百个不同的人这个问题,你会得到一百个不同的答案。我喜欢使编写/维护代码最简单的任何方法,其中一半是长描述性名称,另一半是简短而甜美的名称。只要代码直观且灵活,我看不出任何一种方式有问题。

于 2010-02-09T20:51:10.203 回答
3

有时您必须使用较长的名称,但您通常希望名称尽可能短。关键是要有描述性的名称,提供足够的细节,仅此而已。

于 2010-02-09T20:52:48.720 回答
2

PreferredTargetLocationPresenter.PreferredTargetLocation类型的子PreferredTargetLocationPresenter类型吗?换句话说,你是在嵌套类吗?

如果是这样,你可能会更好地PreferredTargetLocation闯入自己的班级。这允许您编写:

List<PreferredTargetLocation>

代替

List<PreferredTargetLocationPresenter.PreferredTargetLocation>

如果您在 C# 3.0 中工作,则可以使用以下方法进一步缩短声明var

var locations = new List<PreferredTargetLocation>();
于 2010-02-09T20:53:39.233 回答