有什么比“经理”、“处理者”等更好的名字呢?

发布于 2024-10-30 22:56:41 字数 1699 浏览 6 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

滥情哥ㄟ 2024-11-06 22:56:41

等待!

这本书的要点是,类名中的 Manager 意味着该类不仅仅做一件事。 Robert C. Martin 在本节中提到了单一职责原则!

这与名称无关,而是与通常这样命名的类有关。更改名称并不会减少班级的责任!

Wait!

The point of the book is, that Manager in the classname means the class does more than one thing. Robert C. Martin refers in this section to the Single Responsibility Principle!

This is not about the name, its about the classes which are usually named like this. Changing the name won't reduce the responsibilities of a class!

山色无中 2024-11-06 22:56:41

我猜这本书之所以提出这一点,是因为它试图鼓励您为您的班级选择一个更具描述性的名称。这里没有可遵循的“命名约定”;这就是你首先遇到的问题。任何通用命名约定都无法考虑特定的类并为其选择最佳名称。表达能力比遵循命名约定更重要。将一个类称为“管理器”或“处理器”并不能说明它对于第一次阅读您的代码的人来说以及它的作用。

如果您实在想不出更好的办法,那么将类命名为 ConnectionManager 并没有什么本质上的错误。但我至少会以其管理的集合的类型来命名它。或者也许如何管理这些集合。或者为什么它管理这些集合。

还要考虑到,遵循“一刀切”的规则很少能帮助任何人编写更好的代码(至少,在“更易于理解”或“更具表现力”的意义上不是更好)。我倾向于在我所有的名字后加上后缀。例如,我可能有名为 DwmManagerVisualStylesManager 的类,在特定情况下,它。如果我在我的代码库中看到一个名为 Manager 的类,我就知道它包含了一堆密切相关的功能,这对我来说确实意味着什么。根据具体情况,了解您最终想要完成的任务

如果您阅读了《代码大全》并错过了有关编写清晰易懂(因此可维护)的代码的部分,那么您可能错过了。观点。

My guess is the book makes this point because it's trying to encourage you to choose a more descriptive name for your class. There's no "naming convention" to follow here; that's the problem you fell into in the first place. Any universal naming convention won't be able to consider the specific class and choose the best name for it. Expressivity is more important than following a naming convention. Calling a class a "Manager" or a "Processor" doesn't say very much about it and what it does to a person who is reading your code for the first time.

If you really can't think of anything better, there's nothing inherently wrong with naming a class ConnectionManager. But I'd at least name it after the type of collections that it manages. Or maybe how it manages those collections. Or why it manages those collections.

Also consider that following "one-size-fits-all" rules rarely helped anyone to write better code (at least, not better in the sense of "more understandable" or "more expressive). I tend to postfix the names of all my native wrapper classes with Manager. For example, I might have classes called DwmManager, or VisualStylesManager. In that very specific case, it does mean something to me. If I see a class named Manager in my code base, I know it wraps a bunch of closely-related functionality. You have to make the decision on a case-by-case basis, understanding what you're ultimately trying to accomplish.

If you read Code Complete and missed the part about writing code that's clear and understandable (and therefore maintainable), you might have missed the point.

瀟灑尐姊 2024-11-06 22:56:41

在 ConnectionManager 类的示例中,该类可能更能表明设计缺陷,需要一个“坏”名称。除了执行可以在名为 MyDatabaseConnection 的单例子类中轻松完成的操作之外,该类是否还执行其他操作?

In the example of your ConnectionManager class, it is possible that the class is more indicative of a design flaw which necessitates a "bad" name. Is the class doing something besides performing actions that could just as easily be done in a singleton subclass called MyDatabaseConnection?

美人如玉 2024-11-06 22:56:41

哦,不!

请不要被关于“应该”如何命名事物的一般性声明吓倒。毕竟,它们是你的东西。也许您的名字“Manager”比“Connection Manager”更好,因为它更短。特别是如果您的代码的该部分主要管理“连接”而没有其他内容的话。

我相信这些书有非常有用的想法,但只适合那些永远不会读它们的程序员。为什么?因为读过这些书的工程师已经内化并融入了其中最合理的原则。不读它们的程序员也不在乎。

OH, NO !!

Please don't be intimidated by general pronouncements about how you "should" name things. They are your things, after all. Maybe your name "Manager" is better than "Connection Manager" because it's shorter. Especially so if that section of your code mainly manages "connections" and little else.

I believe that such books have very useful ideas, but only for coders who will never read them. Why? Because engineers who do read such books have already internalized and incorporated the soundest of the principles in them. And coders who won't read them don't care.

老子叫无熙 2024-11-06 22:56:41

以名称 DataProcessor 为例。

首先,每个函数确实都与数据一起工作,获取数据或返回一些数据,除非是,

void f(void)

但如果它是这种类型的函数,它绝对会产生一些东西,所以无论如何它都是处理器。

DataProcessor 基本上是任何东西,因为它没有说明它操作什么以及如何操作。因此,您应该将

DataProcessor 替换为 UserInfoStore。

Take for instance name DataProcessor.

First it is true that each function works with data, takes data or return some data unless is

void f(void)

But if it is such type of function it absolutely makes something so it is Processor in any case.

DataProcessor is basically anything because it does no say what it manipulates and how. So you should replace

DataProcessor with UserInfoStore for instance.

一笑百媚生 2024-11-06 22:56:41

我倾向于经常使用“初始化”或“初始化器”这个词。如果是其他的话,我通常会想到一个比我首先想到的要长的同义词。我还使用术语“解析器”和其他东西。

I tend to use the word "Initialize" or "Initializer" quite frequently. If it's anything else, I usually think of a synonym that is really long compared to the first thing I thought of. I also use the term "Parser" and other things.

舞袖。长 2024-11-06 22:56:41

“助手”、“通用”、“核心”、“实用程序”似乎与“经理”相同或最差。
他们对自己所做的事情只字不提。
“工厂”在这里绝对是错误的。
替代名称是“协调员”或“控制器”。

你说:我有一个类负责启动和停止连接。所以我将其命名为ConnectionManager。

如果类只是“启动”和“停止”连接,为什么不使用“ConnectionSwitcher”?


你说:我到处都用过这些。

我也有很多*经理课程。今天我决定更改“客户经理”的名称。它使用帐户存储库来执行 CRUD 操作。

我将其分为“帐户安全”和“帐户操作”。

它们存在于 MyProject.BusinessLogic 命名空间中,并且都使用公开 CRUD 方法的 AccountRepository 类。
前者负责“登录”和“更改密码”等操作。
后者负责针对数据库的 CRUD 操作,但受某些逻辑的控制,例如,如果帐户存在某些注册表记录,则无法删除。 (“AccountOperations”还使用其他存储库来检索所需的信息)

我对“操作”不满意,我正在寻找更好的东西(协调员、主管......),但这只是一个开始。

我的案例就是一个思考方式的例子。

"Helper", "Common", "Core", "Utilities" seems equal or worst than "Manager".
Them don't say nothing about what they do.
"Factory" is absolutely wrong here.
Alternative names are "Coordinator" or "Controller".

yous said: I have a class that is responsible for starting and stopping connections. So I named it ConnectionManager.

If the class just "start" and "stop" the connection, why not "ConnectionSwitcher" ?


you said: I have used these everywhere.

I have many *Manager classes too. Today I decided to change the name of "AccountManager". It uses the Account repository to perform CRUD operations.

I splitted it in "AccountSecurity" and "AccountOperations".

Them exists in MyProject.BusinessLogic namespace and both uses AccountRepository class that expose the CRUD methods.
The former is responsible for actions like "Login" and "ChangePassword".
The latter is responsible for CRUD operations against the database but regulated by some logic, for example if exists some registry records for the Account it cannot be deleted. (The "AccountOperations" uses also other repositories to retrieve needed informations)

I'm not happy with "operations" and I'm searching something better (Coordinator, Supervisor...) but it is a start.

My case would be an example of the way to think at it.

暖风昔人 2024-11-06 22:56:41

不要太在意这个名字,不要太在意它,除非你或某人真的不喜欢它。

dont be too concerned about the name, dont pay much attention to it unless you or someone really dont like it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文