什么是一些类名称,将表明需要重构?
我遇到了一些文章,如这一个,其中建议有些话不应该被用来作为类名称的一部分。当一个类有一个这样的名字,这意味着代码应该被重构或重新设计。
I came across a few articles like this one, which suggest that some words should never be used as part of a class name. When a class has one of those words in the name, it means the code should be refactored or redesigned.
示例:
经理
原因:由于几乎所有课程都管理某事,经理的意义非常广泛,对经理课程负有很大的责任,同时仍然可以要求课堂只有一件事。其结果是,命名一类具有经理人也不多说什么类的实际执行。前面提到的文章,命名没有经理的Java类,显示了这一点:
Reason: Since almost all classes "manage" something and the meaning of "Manager" is very broad, people can put a lot of responsibilities to a "Manager" class while still being able to claim the class does "only one thing". As a result, naming a class with "Manager" does not say much about what the class actually does. The article mentioned earlier, "Naming Java Classes Without a 'Manager' ", showed this point:
例如,拿一个名为UrlManager的类 - 你不知道它是否池URL,操纵URL或审核使用它们。所有的名字告诉你的是,这不是一个URL,但它不以某种方式与他们合作。另一方面,名称UrlBuilder给出了一个更好的图片,该类是什么。
For instance, take a class named "UrlManager" - you cannot tell whether it pool URLs, manipulates URLs or audits the use of them. All the name tells you is that this is not a URL, but it does somehow work with them. On the other hand, the name "UrlBuilder" gives a much better picture of what the class does.
另一个例子:
帮助
原因:类名称如ThreadHelper让人想知道为什么需要为什么它不能仅仅是主题类的一部分。它实际上是适配器还是装饰器?如果是这样,就这样命名。班级线程已经承担了太多责任吗?如果是这样,重构并给新课堂一个有意义的名字。 帮助者没有说明它正在做什么或如何帮助。
Reason: A class name like "ThreadHelper" makes people wonder why it's needed and why it cannot just be part of the "Thread" class. Is it actually an adapter or a decorator? If so, name it that way. Is class "Thread" taking too much responsibility already? If so, refactor and give the new class a meaningful name. "Helper" says nothing about what it's doing or how it's helping.
什么是类名中的其他单词,表示需要重构或重新设计,应该避免?为什么?
What are other words in a class name that would signal a need for refactoring or redesign and should be avoided? Why?
编辑:自从
-
- 他们可以适应几乎所有的环境
- 他们阻止设计师思考更好的设计或名称
- 人们认为可以使用它们
- they generally have broad meanings
- they can fit in almost all contexts
- they stop designers thinking about better designs or names
- people believe it's OK to use them
这本书 清洁代码
避免使用像课程名称的经理,处理器,数据或信息等字。
Avoid words like Manager, Processor, Data, or Info in the name of a class.
它如果有人可能为他们提供可能的理由,那将是巨大的。
It would be great if someone could provide possible reasons for them.
相关问题:
Related questions: