我应该避免使用常用的类名吗?

huangapple go评论92阅读模式
英文:

Should I avoid commonly used class names?

问题

有些类名非常“通用”,以至于它们经常出现在不同的包中,包括库和应用程序代码中。以下是一些例子:

  • Comment(注释)
  • Component(组件)
  • Factory(工厂)
  • Location(位置)
  • Region(区域)

在我的集成开发环境中,尝试自动完成类似这些类的导入时会出现多个竞争性的建议。

在命名类时,是否应该避免已在其他地方使用过的类名呢?

对于其中的一些例子,我想象使用这样的类名是不被鼓励的,因为它们的含义不够明确(例如,Factory),但我想知道是否因为在其他地方(频繁地)使用过这样的类名而不鼓励使用它们。

英文:

Some class names are so "generic" that they are often found in several different packages, including in libraries and application code. Some examples:

  • Comment
  • Component
  • Factory
  • Location
  • Region

In my IDE, attempting to auto-complete the import for a class like one of these summons several competing suggestions.

When naming classes, is it a good idea to avoid class names already used elsewhere?

For some of these examples, I would imagine that using such class name is discouraged because it is simply not meaningful enough (e.g. Factory), but I am wondering whether it is discouraged to use a class name because it is used (frequently) elsewhere.

答案1

得分: 2

你应该根据自己的实际情况使用类名。你提出的类名并没有限制,没有理由不能使用它们(假设支持命名空间并且可以避免这种方式的命名冲突)。

然而,你可以考虑使用更具体和精准的类名,这样能更好地描述代码中的对象含义。例如:

  • Comment(注释):在编译器项目中,你可能会使用LineComment(行注释)或BreakComment(段落注释)作为类名,以便为注释创建语义块。
  • Component(组件):在实现 UI 库并使用基于类的组件时,ListComponent(列表组件)、CalendarComponent(日历组件)或ViewComponent(视图组件)会更合适。
  • Factory(工厂):如果你要制作比萨,那么PizzaFactory(比萨工厂)会更合理。
  • Location(位置):在实现基于方向的导航应用时,区分‘45°N,77°W’和‘比萨店旁边’等情况时,GeographicLocation(地理位置)或SemanticLocation(语义位置)会更合适。
  • Region(区域):在编译器中可以使用CodeRegion(代码区域),在地图应用中可以使用GeographicRegion(地理区域)。

如果你担心具体性,命名空间和包可以提供帮助。然而,并没有什么阻止你在合适的情况下使用与其他包相同的类名。特定的类名并没有版权,大多数集成开发环境现在足够智能,可以区分在自动完成时引用的是哪个包。

总的来说,具体的命名有助于其他开发人员阅读你的代码,这是每个开发者都能欣赏的!

英文:

You should use class names where they make the most sense for you. None of the names above that you've proposed are off limits, and there's no reason why you can't use them (assuming a language that supports namespaces and can avoid naming conflicts in this way).

However, you may consider drilling down to class names that are more specific and precise, which will better describe the meaning of the objects in your code. For example:

  • Instead of Comment: LineComment or BreakComment could easily be class names in a compiler project where you would like to create semantic blocks for comments.
  • Instead of Component: ListComponent, CalendarComponent, or ViewComponent make particular sense when implementing a UI library where you have class-based components.
  • Instead of Factory: PizzaFactory makes more sense if you're trying to make pizzas!
  • Instead of Location: GeographicLocation or SemanticLocation makes more sense when implementing a directions based navigation app, and you're trying to distinguish between '45 deg N, 77 deg W' and 'next to the pizza place'.
  • Region: CodeRegion could be used in a compiler, and GeographicRegion could be used in a Maps app.

If you're afraid to be specific, namespaces and packages help. However, there is nothing discouraging you from using the same name for a class as another package where it makes sense. The class names specifically aren't copyrighted, and most IDEs now are smart enough to make distinctions between what packages you're referring to when using autocompletion.

For the most part, specificity is helpful in assisting other developers to read your code, which every developer can appreciate!

答案2

得分: 1

评论,区域和位置看起来都还不错。就个人而言,主观上说,"Component" 和 "Factory" 这两个词用起来肯定太普通了,但客观上我想不出任何传统理由不使用它们作为名称。我肯定会尝试将这些名称与它们各自的用途联系起来,例如:TaskFactory,WidgetComponent,ButtonFactory 等。

英文:

Comment, Region, and Location seem fine. Personally, so subjectively, Component and Factory are definitely too common to use but objectively I can't think of any conventional reason not to use them as names. I'd definitely try and couple those names with their respective usage, for example; TaskFactory, WidgetComponent, ButtonFactory, etc.

答案3

得分: 0

在技术部分:使用常见的命名实际上是让其他人了解所使用模式的一种方式,Factory 是一个很好的例子 - 当你看到一个类的命名类似于 SomethingFactory,你可以预期会涉及到工厂模式。这在框架、库等进一步延伸 - 在 Spring Boot 中有 SomethingAutoConfiguration,在 JPA 中有 SomethingEntity,在前端框架(React、Angular)中 Component 是一个非常常见的词。所以,是的,尽管放心使用,只要你使用得正确。

在业务部分:简单来说,如果这些词描述了你的业务领域,那么毫无疑问可以使用它们。不要因为这些词看起来常见,就试图创造一些花哨的名字(或使用同义词词典!),这是你的业务领域 - 它是神圣的。

英文:

Depends if we are talking about business or technical part.

In technical part: using common names is actually a way to let others know about the patterns used, Factory is a good example - when you see a class named like SomethingFactory, you can expect a Factory Pattern. It goes further to frameworks, libraries etc. - SomethingAutoConfiguration with Spring-Boot, SomethingEntity with JPA, I think with frontend frameworks (React, Angular) Component is a really common word. So ye, by all means, use them, as long as you use them correctly.

In business part: simple, if those words describe your business domain, then by all means use them. Don't try to invent some fancy names (or thesaurus!) just because the words seem common, it's your business domain - it's sacred.

huangapple
  • 本文由 发表于 2020年8月27日 06:50:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63606777.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定